none
[C#] Apply Share Permission for multi-computers RRS feed

  • Question

  • Hello

    I would like to add many computers into a share permission. But my function allow only one computer or account. I would like to know if there are properties or somethings else to allow many computers into share permission ?

    If I play again my function, the first computer will be removed and the new one added, but I would like both.

    Thanks for your help :)

    public void GrantShare(string domain, string computername) { using (DirectoryEntry entry = new DirectoryEntry("LDAP://"+domain)) { using (DirectorySearcher mySearcher = new DirectorySearcher(entry)) { mySearcher.Filter = "(&(objectClass=computer)(cn="+ computername+"))"; mySearcher.SizeLimit = 0; mySearcher.PageSize = 250; mySearcher.PropertiesToLoad.Add("objectSid"); foreach (SearchResult resEnt in mySearcher.FindAll()) { si = new SecurityIdentifier((byte[])resEnt.Properties["objectSid"][0], 0); } } } ManagementObject userTrustee = new ManagementClass(new ManagementPath("Win32_Trustee"), null); userTrustee["Name"] = computername; byte[] utenteSIDArray = new byte[si.BinaryLength]; si.GetBinaryForm(utenteSIDArray, 0); userTrustee["SID"] = utenteSIDArray; ManagementObject userACE = new ManagementClass(new ManagementPath("Win32_Ace"), null); userACE["AccessMask"] = 2032127; userACE["AceFlags"] = AceFlags.ObjectInherit | AceFlags.ContainerInherit; userACE["AceType"] = AceType.AccessAllowed; userACE["Trustee"] = userTrustee; ManagementObject userSecurityDescriptor = new ManagementClass(new ManagementPath("Win32_SecurityDescriptor"), null); userSecurityDescriptor["ControlFlags"] = 4; userSecurityDescriptor["DACL"] = new object[] { userACE }; ManagementClass mc = new ManagementClass("Win32_Share"); ManagementObject share = new ManagementObject(mc.Path + ".Name='MyShare'"); share.InvokeMethod("SetShareInfo", new object[] {null, "Share For ", userSecurityDescriptor }); }




    • Edited by Twenty94470 Thursday, January 17, 2019 9:05 AM
    • Moved by Bill_Stewart Friday, March 15, 2019 5:41 PM Off-topic
    Thursday, January 17, 2019 9:04 AM

All replies

  • This is NOT a C# forum.  Please post in C# developers forum.


    \_(ツ)_/

    Thursday, January 17, 2019 9:26 AM
  • Hint: Get current DACL and add it to the ACE array before setting the SD.


    \_(ツ)_/

    Thursday, January 17, 2019 9:29 AM
  • Hello, Thanks for the tip and the remark, I search on my own with your information and post to C# developers forum :) 
    Thursday, January 17, 2019 11:58 AM