开发者

C# code to set a remote share to inherit permissions from its parent directory

开发者 https://www.devze.com 2023-04-02 08:04 出处:网络
I have two machines, call them client and server, in a Windows domain. The server has a shared directory which can be accessed from the client machine. I want to run a C# application on the client whi

I have two machines, call them client and server, in a Windows domain. The server has a shared directory which can be accessed from the client machine. I want to run a C# application on the client which sets the permission on this share to inherit the permissions of the share's parent directory on the server. How do I do this?

I have tried code along the following lines, but I don't think it has the right effect:

DirectoryInfo shareDirectoryInfo = new DirectoryInfo("\\server\share");
开发者_如何转开发DirectorySecurity directorySecurity = shareDirectoryInfo.GetAccessControl();
directorySecurity.SetAccessRuleProtection(false, false);
InheritanceFlags iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
FileSystemAccessRule accessRule = new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, iFlags, PropagationFlags.InheritOnly, AccessControlType.Allow);
bool modified;
directorySecurity.ModifyAccessRule(AccessControlModification.Set, accessRule, out modified);
if (modified)
{
    Directory.SetAccessControl(name, directorySecurity);
}

I guess I don't understand why I have to create a FileSystemAccessRule for the directory - how can I just say inherit from parent?

Thanks for any help! Martin


You can set the folder to inherit from parent by using SetAccessRuleProtection

DirectoryInfo targetFolder = new DirectoryInfo(@"\\server\share");
DirectorySecurity folderSecurity = targetFolder.GetAccessControl();   // Existing security
folderSecurity.SetAccessRuleProtection(false, true);                // This sets the folder to inherit
targetFolder.SetAccessControl(folderSecurity);

EDIT: The msdn document explains that if false is sent as the first argument, then the second argument is ignored.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号