简体   繁体   English

C#代码,用于设置远程共享以从其父目录继承权限

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

I have two machines, call them client and server, in a Windows domain. 我在Windows域中有两台计算机,称为客户端和服务器。 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. 我想在客户端上运行C#应用程序,该应用程序设置此共享的权限以继承服务器上共享的父目录的权限。 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? 我想我不明白为什么我必须为目录创建一个FileSystemAccessRule - 我怎么能说从父进行继承呢?

Thanks for any help! 谢谢你的帮助! Martin 马丁

You can set the folder to inherit from parent by using SetAccessRuleProtection 您可以使用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. 编辑:msdn文档解释如果将false作为第一个参数发送,则忽略第二个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM