0

I don't understand why I get UnauthorizedAccessException on call to instantiate StreamWriter class.

string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
using (StreamWriter sw = new StreamWriter(path, false ))
{
    // some processing here
}

I ran it from Visual Studio with VS been launched as Administrator. I then run application itself as Administrator. I tried it on a Windows 7 and a Windows 2012 R2 machine and same result. I feel like I am doing something silly here. Can any one point out what that may be?

whoami
  • 1,689
  • 3
  • 22
  • 45
  • 5
    You are trying to write to a folder. `StreamWriter` operates on files. – vcsjones Jun 09 '15 at 21:34
  • 1
    Why do you expect creation of file with path to folder to succeed? In Windows (and other OS) folders and file names share the same space and there can't be file and folder with the same name... – Alexei Levenkov Jun 09 '15 at 21:35
  • `new StreamWriter(Path.Combine(path, "filename.txt"), false )` – Sam I am says Reinstate Monica Jun 09 '15 at 21:36
  • 1
    Your first response to an unexpected exception should be to start debugging. The only relevant variable here is 'path'. Inspect its value, verify using the Explorer to see that is what you expect. – CodeCaster Jun 09 '15 at 21:39

0 Answers0