tags:

views:

36

answers:

1

Is there any way to make a folder on a remote machine as writable. i have the username and password of remote machine with admin privileges. I want to make that folder as writable programmatically. i prefer c# to do it

+2  A: 

You can use the DirectorySecurity class to change the folder access privileges:

        // Create a new DirectoryInfo object corresponding to the remote folder.
        DirectoryInfo dirInfo = new DirectoryInfo("remoteDirectoryPath");

        // Get a DirectorySecurity object that represents the current security settings.
        DirectorySecurity dirSecurity = dirInfo.GetAccessControl();

        string user = "domain\\userForWhichTheRightsAreChanged";

        // add the write rule for the remote directory
        dirSecurity.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Write, AccessControlType.Allow));

        // Set the new access settings.
        dirInfo.SetAccessControl(dirSecurity);

If your code does not run under the account that has administrative privileges on the remote machine, please also consider using impersonation. A complete sample about how to impersonate a user is available here.

andrei m
thanks for your reply. i tried to impersonate. i can login to my own local machine. but if i am trying to access a remote machine (i passed domain name like 192.168.0.25) it could not logging. i tried to login on many machines. but it shows same error-->"Logon failure: unknown user name or bad password:. But i can login to my own local machine
Shameer
Could you confirm that you are trying to impersonate the user that has administrative privileges on the remote machine?
andrei m
yeah. i cross checked it again. if i gave a wrong domain address,it still shows same error messageor-->"Logon failure: unknown user name or bad password:. But i can login to my own local machine ".i can login to my system .
Shameer
I suggest to check if your account and credentials for the remote machine are correct. Could you try to access the remote machine using windows explorer and provide this account to see if it works?
andrei m
If your remote computer is not connected to a domain I think that you should try to pass an empty string for the domain parameter when impersonating.
andrei m
thanks for your helps :)
Shameer