1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| // Create a ManagementPath and scope object that will control
// the creating of the share
ManagementPath path = new ManagementPath();
path.Server = System.Environment.MachineName; // Machine name of the local computer
path.NamespacePath = @"root\CIMV2";
ManagementScope scope = new ManagementScope(path);
NTAccount ntAccount1 = new NTAccount("Domain1", "MainDevGroup");
SecurityIdentifier sid1 = (SecurityIdentifier)ntAccount1.Translate(typeof(SecurityIdentifier));
byte[] sidArray = new byte[sid1.BinaryLength];
sid1.GetBinaryForm(sidArray, 0);
//ManagementObject to represent the group to be added as a trustee
ManagementObject newGroup1 = new ManagementClass(scope, new ManagementPath("Win32_Trustee"), null).CreateInstance();
newGroup1["Domain"] = "Domain1";
newGroup1["Name"] = "MainDevGroup";
newGroup1["SID"] = sidArray;
NTAccount ntAccount2 = new NTAccount("Domain1", "MainEngGroup");
SecurityIdentifier sid2 = (SecurityIdentifier)ntAccount2.Translate(typeof(SecurityIdentifier));
byte[] sidArray2 = new byte[sid2.BinaryLength];
sid2.GetBinaryForm(sidArray2, 0);
//ManagementObject to represent the group to be added as a trustee
ManagementObject newGroup2 = new ManagementClass(scope, new ManagementPath("Win32_Trustee"), null).CreateInstance();
newGroup2["Domain"] = "Domain1";
newGroup2["Name"] = "MainEngGroup";
newGroup2["SID"] = sidArray2;
// ManagementObject to represent the new group to add to the
// acle of the share
ManagementObject aceGroup1 = new ManagementClass(scope, new ManagementPath("Win32_ACE"), null).CreateInstance();
aceGroup1["AccessMask"] = 2032127;
aceGroup1["AceFlags"] = 3;
aceGroup1["AceType"] = 0;
aceGroup1["Trustee"] = newGroup1;
ManagementObject aceGroup2 = new ManagementClass(scope, new ManagementPath("Win32_ACE"), null).CreateInstance();
aceGroup2["AccessMask"] = 2032127;
aceGroup2["AceFlags"] = 3;
aceGroup2["AceType"] = 0;
aceGroup2["Trustee"] = newGroup2;
// Management class to modify the ACL
ManagementObject secDescriptor = new ManagementClass(scope,
new ManagementPath("Win32_SecurityDescriptor"), null).CreateInstance();
secDescriptor["ControlFlags"] = 4;
secDescriptor["DACL"] = new ManagementObject[] { aceGroup1, aceGroup2 };
// Management class to modify creation of the share itself
ManagementObject mc = new System.Management.ManagementClass(scope, new ManagementPath("Win32_Share"), null);
Directory.CreateDirectory(@"D:\TempFolder");
System.Management.ManagementBaseObject inParams = mc.GetMethodParameters("Create");
inParams["Path"] = @"D:\TempFolder";
inParams["Name"] = "TempFolderShare";
inParams["Type"] = 0;
inParams["Access"] = secDescriptor;
inParams["Description"] = "Test";
//inParams["Access"] = null;
System.Management.ManagementBaseObject mbo = mc.InvokeMethod("Create", inParams, null);
uint result = (uint)mbo.Properties["ReturnValue"].Value;
// 0 = success |
Partager