Null Pointer
Null Pointer

Reputation: 9289

make a folder on remote machine as writable

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

Upvotes: 0

Views: 2410

Answers (1)

andrei m
andrei m

Reputation: 1157

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.

Upvotes: 3

Related Questions