简体   繁体   中英

StreamWriter doesn't respond

I am trying to create a program that edits a few lines in a text file, to a user set value, from a textbox.

At least, before I even bothered to start with this value setting. I can't even edit the file. What's wrong with this code? I actually tried more examples, but none of them worked.

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

Sorry for my bad english.

An educated guess.

Try to write your file in a different folder.
The C disk root is write protected by Operating system

for example

using (StreamWriter writer = new StreamWriter("C:\\TEMP\\TEST.txt", true))

or read the Environment.SpecialFolder enum to find an appropriate folder where your application could store its data.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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