views:

758

answers:

6
+1  Q: 

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?

Edit:

The strange thing is 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.

A: 

1) NTFS permissions on the physical directory using explorer

2) Within the IIS MMC console FTP Site to allow read/write on the FTP folder

3) Ensure that the FTP Site or virtual directory actually exists, when checking the above step

http://www.eggheadcafe.com/forumarchives/inetserveriisftp/Jan2006/post25322215.asp

abmv
I believe the error is happening opening the file before it is sent? i.e. FTP site permissions wouldn't be a factor at this point.
russau
A: 

The process that is running your code does not have permissions on the file. Is it part of a web application - if so you need to give access to the ASPNET account.

Give permission to 'everyone' on the file, and see if it still has problems.

simon831
A: 

Is your project being run from a network drive? If so that that will mean it runs in a restricuted priviliges mode that could cause this. Try copying the project to your C drive and running it again.

ck
No i am running it in debug mode from within visual studio from the default local projects directory.I am trying to access the file via it's UNC path e.g. \\server\share\file but aslong as I have the correct permissions (i have Full Access) that should not be a problem right?
+1  A: 

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);
fretje
Yep same account.
A: 

It's near FileSecurity class.

See at FileSecurity class

and try:

        // Get a FileSecurity object that represents the
        // current security settings.
        FileSecurity fSecurity = File.GetAccessControl(localFileName);

        // Add the FileSystemAccessRule to the security settings.
        fSecurity.AddAccessRule(new FileSystemAccessRule("DOMAIN\USERNAME",
            FileSystemRights.ReadData, AccessControlType.Allow));

        // Set the new access settings.
        File.SetAccessControl(localFileName, fSecurity);
Chernikov
That results in: Unhandled Exception: System.Security.Principal.IdentityNotMappedException: Someor all identity references could not be translated.There are domain trusts involved in this setup e.g i am a member of a group in another domain which is granted access to this file, I wonder if that is having an impact on this.
Did you change "DOMAIN\USERNAME" string?
Chernikov
oops i did but forgot to escape the "\", it ran fine now but i am still unable to access the file.
A: 

I ran into the same problem and solved it by using the solution fretje proposed