C#代码来设置远程共享

C#代码来设置远程共享

本文介绍了C#代码来设置远程共享,以继承其父目录的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两台机器,称为客户端和服务器,在Windows域中。服务器具有可以从客户机访问的共享目录。我想在客户端上运行一个C#应用程序,它设置此共享的权限以继承服务器上共享的父目录的权限。我如何做到这一点?

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);
}



我想我不明白为什么我必须创建一个FileSystemAccessRule目录 - 我怎么能说从父母继承?

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

感谢任何帮助! Martin

Thanks for any help! Martin

推荐答案

您可以使用

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);

编辑:msdn文档说明如果false作为第一个参数发送,忽略。

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

这篇关于C#代码来设置远程共享,以继承其父目录的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 15:39