C# – File Access Denied


I am using an FTPClient library to transfer files from a Windows share to an FTP server.

The SendFile method of the library uses the following code:

FileStream stream = new FileStream(localFileName, FileMode.Open);

This results in a System.UnauthorizedAccessException being thrown, however I am able to open, rename, and move the file using Windows Explorer under the same user account which the code is being executed.

Can anyone tell me why this is happening?


The strange thing is that I can access other files on the share which have been granted the same NTFS permissions as the one that I can't.

This is also a Windows forms app.


Still no luck with this. I am able to read the file using a StreamReader but not a file stream. I can't understand why the two behave differently.

Best Solution

Are you sure it's the same user account? Can you try something like



Also, are you sure the file isn't read-only? Do you need write access to the file? Otherwise you could try:

FileStream stream = new FileStream(localFileName, FileMode.Open, FileAccess.Read);