user5813072 user5813072 - 3 months ago 26
C# Question

how to write a file to desktop using streamwriter

I have a block which is supposed to send the overwritten file to my desktop but the code does not seem to be working, I am using a MVC application not a console apllication.

Can anyone tell me what I am doing wrong or advise me on how to achieve my solution.

using (var File = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "~/ColTexOutputFileTest.csv", false)) // true for appending the file and false to overwrite the file
{
foreach (var item in outputFile)
{
File.WriteLine(item);
}
}

Answer

This character ' ~ ' used to find Server Side folder or file

For Example if you access App_Data folder in abc.xml file

HttpContext.Current.Server.MapPath("~/App_Data/abc.xml");

if you streamed file on local access to file as windows path

using (var File = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\ColTexOutputFileTest.csv", false)) // true for appending the file and false to overwrite the file
{
    foreach (var item in outputFile)
    {
        File.WriteLine(item);
    }
}

"~/ColTexOutputFileTest.csv" change it "\ColTexOutputFileTest.csv"