简体   繁体   中英

unauthorisedAccessException when writing to a file

im very new to c# and am trying to write to a file but i get this error. Any suggestions?

    System.UnauthorizedAccessException: 'Access to the path 'C:\Users\tom\Desktop\theBeast\theBeast\bin\Debug' is denied.'
my code:

string hashedValue = Hash(hashed);
string Path = @"\Users\tom\Desktop\theBeast\theBeast\bin\Debug";

using (System.IO.StreamWriter Account = new System.IO.StreamWriter(Path, true))
{
     Account.WriteLine(Username + "," + hashedValue + ",User");
     System.Windows.Forms.MessageBox.Show("Account Successfully Created");
}

You have to take a file path to write something and close stream to save the file. for you try this :

string hashedValue = Hash(hashed);
string Path = @"\Users\tom\Desktop\theBeast\theBeast\bin\Debug\test.txt";

using ( System.IO.StreamWriter Account = new System.IO.StreamWriter( Path , true ) )
            {
                Account.WriteLine( Username + "," + hashedValue + ",User" );
                Account.Close();
                System.Windows.Forms.MessageBox.Show( "Account Successfully Created" );
            }

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