注意:请不要因标题与其他标题相似而忽略。
我正在尝试在 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!");
}
}