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);
在我调用上面的代码之前共享该文件夹已经完成。下面的图像是什么,我得到的结果:
到目前为止,一切都很好。但在下一张图片中,您会看到剩余的两个复选框仍未选中。
我在想什么吗?
谢谢!
编辑:下面是用于做实际共享的代码。
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!");
}
}
我添加了用于执行上述共享的代码。那是我需要修改的吗? – JimDel 2012-04-12 18:53:30
是的。注意:您需要两个片段 - ACL上的文件夹和共享上的ACL。 – 2012-04-12 19:34:41
感谢您的链接,但C++的信息并没有多大帮助。 – JimDel 2012-04-13 14:37:15