注意:请勿根据标题相似而忽略此内容。
我正在尝试在Windows 7机器上共享文件夹,并希望通过C#将其授予所有人完全权限。
我已经看到了其他页面上的几篇文章,包括这里,告诉我如何做。但像其他一些人一样,它对我不起作用。下面是从SO中摘取的片段。
在我调用上面的代码之前,共享文件夹已经完成。下面的图片是我得到的结果:
目前为止,一切都很好。但是在下一个图片中,您将看到剩下的两个复选框仍然未被选中。
我错过了什么吗?
谢谢!
编辑:下面是用于实际共享的代码。
我正在尝试在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](https://istack.dev59.com/37MMB.webp)
![enter image description here](https://istack.dev59.com/JyDmc.webp)
谢谢!
编辑:下面是用于实际共享的代码。
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!");
}
}