System.UnauthorizedAccessException był nieobsługiwany
Otrzymuję wyjątek odmowy dostępu. Jak mogę to naprawić?
Oto wyjątek:
System.UnauthorizedAccessException nie został obsłużony HResult = -2147024891 Wiadomość = Dostęp do ścieżki 'c: message.txt' jest zabroniony.
Źródło = mscorlib
Oto kod:
public static void WriteToFile(string s)
{
fs = new FileStream("c:\\message.txt",
FileMode.Append, FileAccess.Write);
sw = new StreamWriter(fs);
sw.WriteLine(s);
sw.Flush();
sw.Close();
fs.Close();
}
EDYCJA: Działa, gdy uruchomię vs2012 jako administrator, ale czy istnieje sposób lub powód, aby to zrobić jako zwykły użytkownik?
I to działa:
public static void WriteToFile(string s)
{
fs = new FileStream(@"C:\Users\KristjanBEstur\Documents\message.txt",
FileMode.Append, FileAccess.Write);
sw = new StreamWriter(fs);
sw.WriteLine(s);
sw.Flush();
sw.Close();
fs.Close();
File.Delete(@"C:\Users\KristjanBEstur\Documents\message.txt");
}