File.WriteAllText() method is throwing an exception of type 'System.UnauthorizedAccessException' RRS feed

  • Question

  • Hi,

    Following is the piece of code written to download page source of a website whose uri is passed as parameter and then copy this source code to file created on hard-drive. However,  File.WriteAllText() method is throwing - "An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll. Access to the path 'D:\Temp' is denied." I have tried giving full control access to the folder and also running Visual Studio as administrator, but no use. Could you please help, thanks in advance.

           WebClient client = new WebClient();
            string pageSource = client.DownloadString(uri);
            File.WriteAllText(filePath, pageSource);

    Friday, June 22, 2018 11:27 AM

All replies

  • A UAE indicates permission issues. Try using a path that you should have permission to write to and see if it works. 

    You didn't provide any information around the creation of filePath itself so other questions include whether the path is properly formatted, if your app is running in a sandbox where things are locked down, etc. Also, the D: would indicate you have 2 hard drives and a Temp folder on the second one. Are you sure the path is correct?

    Other things I'd look at include using the effective permissions in Explorer to verify the account has permissions and then in the .NET code trying something simpler like simply trying to read a file in that folder first.

    Michael Taylor

    Friday, June 22, 2018 1:46 PM
  • Does "filePath" literally contain "D:\Temp"?  And is that a folder?  If so, then that's the problem.  You can't write text to a folder.  filePath needs to contain the name of a file within that folder, not the folder name itself.

    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    • Proposed as answer by WayneAKing Sunday, June 24, 2018 4:01 AM
    Friday, June 22, 2018 7:08 PM
  • Thanks Tim, that worked! I was using folder path instead of a file name.

    Saturday, June 23, 2018 11:49 AM
  • Hi Bharath Gaddam,

    If your question has been solved, please mark the useful reply as answer. This will make answer searching easier in the forum and be beneficial to community members.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Monday, June 25, 2018 6:50 AM