简体   繁体   English

如何使用Streamwriter将文件写入桌面

[英]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. 我有一个应该将覆盖文件发送到我的桌面的块,但是代码似乎不起作用,我使用的是MVC应用程序,而不是控制台应用程序。

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);
    }
}

删除“〜”字符。

"\ColTexOutputFileTest.csv"

This character ' ~ ' used to find Server Side folder or file 此字符'〜'用于查找服务器端文件夹或文件

For Example if you access App_Data folder in abc.xml file 例如,如果您访问abc.xml文件中的App_Data文件夹

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

if you streamed file on local access to file as windows path 如果您将文件的本地访问流式传输为Windows路径

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" “〜/ ColTexOutputFileTest.csv” 将其更改为 “ \\ ColTexOutputFileTest.csv”

As stated in the answers above, the ~ is the problem. 如以上答案中所述,〜是问题所在。 .Net provides the Path class which has a combine method for joining path & file names & not needing to know whether separators are needed : .Net提供了Path类,该类具有用于合并路径和文件名的Combine方法,并且无需知道是否需要分隔符:

using (var File = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "ColTexOutputFileTest.csv"), false))

See : https://msdn.microsoft.com/en-us/library/system.io.path(v=vs.110).aspx 请参阅: https : //msdn.microsoft.com/zh-cn/library/system.io.path(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM