I am getting a access denied exception. How can I fix this?
Here is the exception:
System.UnauthorizedAccessException was unhandled HResult=-214
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!");
}
}