C# 创建并保存文本文件

C# 创建并保存文本文件,c#,C#,我正在尝试用c#创建并保存文本文件。当我编写代码时,它与localhost的工作非常顺利,但当我在ftp上上传代码时,它就停止了工作 这段代码在本地主机上运行得很好,但在ftp上运行得不好 StreamWriter _testData; _testData = new StreamWriter(@"C:\Users\Public\Downloads\sample.txt", true); _testData.WriteLine(text); // Write the file. _testDat

我正在尝试用c#创建并保存文本文件。当我编写代码时,它与
localhost
的工作非常顺利,但当我在ftp上上传代码时,它就停止了工作

这段代码在本地主机上运行得很好,但在ftp上运行得不好

StreamWriter _testData;
_testData = new StreamWriter(@"C:\Users\Public\Downloads\sample.txt", true);
_testData.WriteLine(text); // Write the file.
_testData.Close(); // Close the instance of StreamWriter.
_testData.Dispose(); // Dispose from memory.

你有什么错误?您的远程服务器上可能没有这样的文件夹
C:\Users\Public\Downloads
?或者,运行应用程序的用户没有写入此文件夹的权限?此外,您不需要
关闭
处置
。使用语句将
StreamWriter
简单地包装在
中就足够了。顺便说一下,如果使用
使用
,您可以使用两行代码来完成这一切:
使用(StreamWriter\u testData=new StreamWriter(@“C:\Users\Public\Downloads\sample.txt”,true)){\u testData.WriteLine(text);}
实际上,他可以在一行代码中完成这一切:
System.IO.File.writealText(@“C:\Users\Public\Downloads\sample.txt”,text):-)无法使用StreamWriter写入FTP站点。你想干什么?您是否还具有写入相关文件夹的权限?(尽管它是公开的——仍然值得检查)@Ahmedilyas,他并没有使用StreamWriter向FTP写信。他正在使用FTP将他的web应用程序上载到远程服务器,但在那里,他的应用程序停止工作,因为在该远程服务器上没有他试图写入的文件夹
C:\Users\Public\Downloads