System.UnauthorizedAccessException was unhandled

前端 未结 5 1292
既然无缘
既然无缘 2021-01-22 01:06

I am getting a access denied exception. How can I fix this?

Here is the exception:

System.UnauthorizedAccessException was unhandled HResult=-214

5条回答
  •  Happy的楠姐
    2021-01-22 01:34

    You need to set the FileAtrributes property of the File to Normal before accessing the file.

    Try This:

    public static void WriteToFile(string s)
        {
            String path=@"c:\message.txt";
            if(File.Exists(path))
            {
            File.SetAttributes(path, FileAttributes.Normal);
            fs = new FileStream(path,
            FileMode.Append, FileAccess.Write);
            sw = new StreamWriter(fs);
            sw.WriteLine(s);
            sw.Flush();
            sw.Close();
            fs.Close();
            }
            else
            {
            MessageBox.Show("File not Found!");
            }
        }
    

提交回复
热议问题