1) 目录共享:通过WMI的Win32_Share类的Create()方法来实现。
2) 目录安全:通过WMI的Win32_LogicalFileSecuritySetting类的SetSecurityDescriptor()方法来实现,涉及到win32_trustee和win32_ace类。
完整代码 如下:
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
System.Management;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
namespace
SharePermissions
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public enum ShareResourceType : uint
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
DiskDrive = 0x0,
PrintQueue = 0x1,
Device = 0x2,
IPC = 0x3,
DiskDriveAdmin = 0x80000000,
PrintQueueAdmin = 0x80000001,
DeviceAdmin = 0x80000002,
IPCAdmin = 0x80000003
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
[Flags]
public enum AccessPrivileges : uint
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
FileReadData = 0x00000001,
FileWriteData = 0x00000002,
FileAppendData = 0x00000004,
FileReadEA = 0x00000008,
FileWriteEA = 0x00000010,
FileExecute = 0x00000020,
FileDeleteChild = 0x00000040,
FileReadAttributes = 0x00000080,
FileWriteAttributes = 0x00000100,
Delete = 0x00010000,
ReadControl = 0x00020000,
WriteDac = 0x00040000,
WriteOwner = 0x00080000,
Synchronize = 0x00100000
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
[Flags]
enum AceFlags : uint
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
NonInheritAce = 0,
ObjectInheritAce = 1,
ContainerInheritAce = 2,
NoPropagateInheritAce = 4,
InheritOnlyAce = 8,
InheritedAce = 16
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
[Flags]
enum AceType : uint
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
AccessAllowed = 0,
AccessDenied = 1,
Audit = 2
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public partial class Form1 : Form
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
public Form1()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
InitializeComponent();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private void Form1_Load(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
SetShares();
SetPermissions();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
void SetShares()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
ManagementClass mc = new ManagementClass("win32_share");
ManagementBaseObject inParams = mc.GetMethodParameters("Create");
inParams["Path"] = @"C:DownloadsTemp";
inParams["Name"] = "Temp";
inParams["Type"] = ShareResourceType.DiskDrive;
inParams["MaximumAllowed"] = null; //null = 用户数连接无限制,否则指定一个正整数
inParams["Description"] = null;
inParams["Password"] = null;
inParams["Access"] = null; //null = 使Everyone拥有完全控制权限
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
ManagementBaseObject outParams = mc.InvokeMethod("Create", inParams, null);
uint returnValue = (uint)outParams.Properties["ReturnValue"].Value;
string ErrorMessage = null;
switch (returnValue)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
case 0: //Success
break;
case 2: //Access denied
ErrorMessage = "无权访问";
break;
case 8: //Unknown failure
ErrorMessage = "未知失败";
break;
case 9: //Invalid name
ErrorMessage = "非法的共享名";
break;
case 10: //Invalid level
ErrorMessage = "非法的层次";
break;
case 21: //Invalid parameter
ErrorMessage = "非法的参数";
break;
case 22: //Duplicate share
ErrorMessage = "重复共享";
break;
case 23: //Redirected path
ErrorMessage = "重定向路径";
break;
case 24: //Unknown device or directory
ErrorMessage = "未知的目录";
break;
case 25: //Net name not found
ErrorMessage = "网络名不存在";
break;
default:
break;
}
if (returnValue == 0)
MessageBox.Show("成功");
else
MessageBox.Show(ErrorMessage);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
bool SetPermissions()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//ManagementPath path = new ManagementPath();
//path.Server = "."; // 机器名, .表示本机
//path.NamespacePath = @"rootcimv2";
//path.RelativePath = string.Format("Win32_LogicalFileSecuritySetting.Path='{0}'", @"C:DownloadsTemp");
//ManagementObject mo = new ManagementObject(path);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
ManagementObject mo = new ManagementObject(string.Format("Win32_LogicalFileSecuritySetting.Path='{0}'", @"C:DownloadsTemp"));
ManagementBaseObject outParams = mo.InvokeMethod("GetSecurityDescriptor", null, null);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if ((uint)outParams.Properties["ReturnValue"].Value != 0)
return false;
ManagementBaseObject Descriptor = (ManagementBaseObject)outParams.Properties["Descriptor"].Value;
//foreach (ManagementBaseObject mbo in (ManagementBaseObject[])Descriptor.Properties["Dacl"].Value)
//{
// this.richTextBox1.AppendText(string.Format("AccessMask: {0} AceFlags:{1} AceType: {2} ", mbo["AccessMask"],
// Enum.Format(typeof(AceFlags), mbo["AceFlags"], "g"), mbo["AceType"]));
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// if ((uint)mbo["AceType"] == (uint)AceType.AccessDenied)
// this.richTextBox1.AppendText("Denied Ace ");
// else
// this.richTextBox1.AppendText("Allowed Ace ");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// ManagementBaseObject Trustee = ((ManagementBaseObject)(mbo["Trustee"]));
// this.richTextBox1.AppendText(string.Format("Name: {0} Domain: {1} SID: {2} ",
// Trustee.Properties["Name"].Value,
// Trustee.Properties["Domain"].Value,
// Trustee.Properties["SIDString"].Value));
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// uint mask = (UInt32)mbo["AccessMask"];
// this.richTextBox1.AppendText(Enum.Format(typeof(AccessPrivileges), mask, "g"));
// this.richTextBox1.AppendText(" ");
//}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
List<ManagementBaseObject> newDacl = new List<ManagementBaseObject>();
//newDacl.AddRange((ManagementBaseObject[])Descriptor.Properties["Dacl"].Value);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//增加Everyone用户
ManagementClass trustee = new ManagementClass("win32_trustee");
trustee.Properties["Name"].Value = "Everyone";
trustee.Properties["Domain"].Value = null;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//设置只读/运行权限
ManagementClass ace = new ManagementClass("win32_ace");
ace.Properties["AccessMask"].Value = AccessPrivileges.FileReadData | AccessPrivileges.FileReadAttributes | AccessPrivileges.FileReadEA
| AccessPrivileges.ReadControl | AccessPrivileges.FileExecute;
ace.Properties["AceFlags"].Value = AceFlags.ObjectInheritAce | AceFlags.ContainerInheritAce | AceFlags.NoPropagateInheritAce;
ace.Properties["AceType"].Value = AceType.AccessAllowed;
ace.Properties["Trustee"].Value = trustee;
newDacl.Add(ace);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
ManagementBaseObject inParams = mo.GetMethodParameters("SetSecurityDescriptor");
Descriptor.Properties["Dacl"].Value = newDacl.ToArray();
//Descriptor.Properties["Group"].Value = null;
//Descriptor.Properties["Owner"].Value = null;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
inParams["Descriptor"] = Descriptor;
ManagementBaseObject ret = mo.InvokeMethod("SetSecurityDescriptor", inParams, null);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
uint returnValue = (uint)ret.Properties["ReturnValue"].Value;
string ErrorMessage = null;
switch (returnValue)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
case 0: //Success
break;
case 2: //Access denied
ErrorMessage = "无权访问";
break;
case 8: //Unknown failure
ErrorMessage = "未知失败";
break;
case 9: //Privilege missing
ErrorMessage = "缺少权限";
break;
case 21: //Invalid parameter
ErrorMessage = "非法的参数";
break;
default:
break;
}
if (returnValue == 0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MessageBox.Show("成功");
return true;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MessageBox.Show(ErrorMessage);
return false;
}
}
}
}
另外需要注意的是:当新建目录时,系统会从上级目录自动继承用户,这些用户可能有完全控制权限,当你对某个目录设置只读时,可能由于这些继承用户的存在而影响到最终结果。所以要将根目录(如C:/, d:/等)的目录属性的安全页中将一些系统自动添加的具有完全控制的用户(如Creator Owner, Users, System)删除或改为非继承或仅当前目录有效。
注:根目录的Everyone不会被继承(可以通过目录属性的安全/高级/编辑中看到),另外也不要把本机管理员或域管理员(都是Administrator)误删了。