StreamWriter没有响应

Val*_*ons 2 c#

我正在尝试创建一个程序,从文本框中编辑文本文件中的几行到用户设置值.

至少,在我开始考虑这个值设置之前.我甚至无法编辑该文件.这段代码出了什么问题?我实际上尝试了更多的例子,但没有一个能够奏效.

private void pictureBox2_Click(object sender, EventArgs e) //login button
{
    username = textBox1.Text;
    using (StreamWriter writer = new StreamWriter("C:\\TEST.txt", true))
    {
        writer.WriteLine("Last User:" +username );
    }
    Application.Exit();
}
Run Code Online (Sandbox Code Playgroud)

对不起,我的英语不好.

Ste*_*eve 5

一个有根据的猜测.

尝试将文件写入不同的文件夹.
C盘根由操作系统写保护

例如

using (StreamWriter writer = new StreamWriter("C:\\TEMP\\TEST.txt", true))
Run Code Online (Sandbox Code Playgroud)

或者阅读Environment.SpecialFolder枚举以查找应用程序可以存储其数据的相应文件夹.

string appFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string privateAppFolder = Path.Combine(appFolder, "MyAppFolder");
if(!Directory.Exists(privateAppFolder)) Directory.CreateDirectory(privateAppFolder);
string myFile = Path.Combine(privateAppFolder, "Test.txt");
using (StreamWriter writer = new StreamWriter(myFile, true))
{
    writer.WriteLine("Last User:" +username );
}
Run Code Online (Sandbox Code Playgroud)