2012-04-12 189 views
2

注意:请不要忽视与其他人类似的标题。将“Everyone”权限添加到文件夹的网络共享中

我试图在Windows 7机器上共享文件夹。我想通过C#给每个人完整的权限。

我在其他网页上看过几篇文章,包括这里,告诉我该怎么做。但是和其他人一样,这对我不起作用。以下是摘自SO的摘录。

DirectorySecurity sec = Directory.GetAccessControl(path); 
    // Using this instead of the "Everyone" string means we work on non-English systems. 
    SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null); 
    sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.FullControl | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)); 
    Directory.SetAccessControl(path, sec); 

在我调用上面的代码之前共享该文件夹已经完成。下面的图像是什么,我得到的结果:

enter image description here

到目前为止,一切都很好。但在下一张图片中,您会看到剩余的两个复选框仍未选中。

enter image description here

我在想什么吗?

谢谢!

编辑:下面是用于做实际共享的代码。

private static void QshareFolder(string FolderPath, string ShareName, string Description) 
    { 
     try 
     { 
      ManagementClass managementClass = new ManagementClass("Win32_Share"); 
      ManagementBaseObject inParams = managementClass.GetMethodParameters("Create"); 
      ManagementBaseObject outParams; 

      inParams["Description"] = Description; 
      inParams["Name"] = ShareName; 
      inParams["Path"] = FolderPath; 
      inParams["MaximumAllowed"] = null; 
      inParams["Password"] = null; 
      inParams["Access"] = null; 
      inParams["Type"] = 0x0; // Disk Drive 

      // Invoke the method on the ManagementClass object 
      outParams = managementClass.InvokeMethod("Create", inParams, null); 

      // Check to see if the method invocation was successful 
      if ((uint) (outParams.Properties["ReturnValue"].Value) != 0) 
      { 
       throw new Exception("Unable to share directory."); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message, "error!"); 
     } 
    } 

回答

3

权限的共享和基础文件夹是分开的 - 你的代码中设置ACL对文件/文件夹...所以你缺少对网络共享本身设置ACL的一部分。

当最终通过共享访问文件时,文件权限和共享权限之间的权限最小。

我不知道如何在共享上设置ACL,但这里是一个相关的C++问题,可能是关于如何设置共享权限的好起点:How to create read-only network share programmatically?

+0

我添加了用于执行上述共享的代码。那是我需要修改的吗? – JimDel 2012-04-12 18:53:30

+0

是的。注意:您需要两个片段 - ACL上的文件夹和共享上的ACL。 – 2012-04-12 19:34:41

+0

感谢您的链接,但C++的信息并没有多大帮助。 – JimDel 2012-04-13 14:37:15

相关问题