写入文件时unauthorisedAccessException

时间:2017-10-26 01:41:22

标签: c#

我对c#非常新,我正在尝试写入文件,但是我收到了这个错误。有什么建议吗?

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

1 个答案:

答案 0 :(得分:0)

您必须使用文件路径写入内容并关闭流以保存文件。 你试试这个:

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