C# – File Access Denied

c++file-iopermissions

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?

Edit:

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.

Update:

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

MessageBox.Show(WindowsIdentity.GetCurrent().Name);

?

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);