none
"Access Denied" : Why FileStream works but Streamwriter Doesn't? RRS feed

  • Question

  • Sure enough I'm not the only one that had "Access denied" exception when writing to a directory on my local. I'm using C# .NET4 and Windows 7. There's lots of information out there but it's not really clear to me what's going on:

    Here's what I'm doing.

     

    Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

     System.IO.StreamWriter file = new System.IO.StreamWriter(mydocpath);  

      

     

     

     

    Oddly enough, this why I don't get the reason for the error I can write

    using

     

     

    FileStream

    writeStream = new FileStream(localPath + fileName, FileMode .Create);

    So I don't get while the first approach results in an error while the second one doesn't. It's the same app trying to write to the same directory.

     


    • Edited by DaGuy007 Monday, November 28, 2011 5:20 PM
    • Changed type Martin_XieModerator Wednesday, November 30, 2011 8:10 AM Change to question so that more beginners can search out it.
    Monday, November 28, 2011 5:17 PM

Answers

  • Well, thanks for the very useful basic quiz. It actually helped me find the problem. The file name was missing in the path of the first approach.

    Monday, November 28, 2011 6:36 PM

All replies

  • Is the user the same?

    Is the filename the same?

    Are the file contents the same?

    Does the file already exist in either example, or is is being created for the first time in both instances?

    Monday, November 28, 2011 5:38 PM
  • Well, thanks for the very useful basic quiz. It actually helped me find the problem. The file name was missing in the path of the first approach.

    Monday, November 28, 2011 6:36 PM