3
Answers

Can't create a text file in C#

Osayz Family

Osayz Family

11y
1.2k
1
My app has a Login Form. 
Following code which I created a text file is not correct. I don't know why. 
Error is Access denied in C:\Windows\Apple.txt
And i want this text file to log usernames when user changed his username
Please help me with correct code.
thx C-SharpCorner guys......

using System.IO;

string fileLocation = @"C:\Windows\Apple.txt";
                
if (!File.Exists(fileLocation))
{
       FileStream xFile = new FileStream(fileLocation, FileMode.CreateNew);
       StreamWriter sw = new StreamWriter(xFile);
       sw.Write(this.txtUsername.Text);
       sw.Close();
}
Answers (3)