Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File.WriteAllText throws UnauthorizedAccessException

Doing File.WriteAllText to a remote path throws UnauthorizedAccessException. When I open the file in notepad I can edit it without a problem. The process that's trying to modify the file is running as my own user account, so it should be able to access it.

like image 493
ripper234 Avatar asked Oct 06 '09 15:10

ripper234


3 Answers

The UnauthorizedAccessException is also thrown if the file has the "hidden" attribute set. Don't ask me why...

like image 81
Roman Starkov Avatar answered Nov 11 '22 03:11

Roman Starkov


According to MSDN, UnauthorizedAccessException can also be caused by:

path specified a file that is read-only.

-or- 

This operation is not supported on the current platform.

-or- 

path specified a directory.

Is it possible one of these conditions is the cause of your problem?

like image 21
Jay Riggs Avatar answered Nov 11 '22 02:11

Jay Riggs


I believe you also get this exception (although its not documented) if the file is being locked by another process or thread.

Make sure nothing else has opened the file in a manner that prohibits writing. Notepad is not a good test for seeing if a file is locked, since it will open a locked file (ie: read-only files are fine).

like image 1
Reed Copsey Avatar answered Nov 11 '22 04:11

Reed Copsey