public override void CreateFileShare(NetworkShareInfo share)
        {
            using (ManagementClass managementClass1 = new ManagementClass("Win32_Share"))
            {
                ManagementClass wmiClassTrustee = new ManagementClass("Win32_Trustee");
                try
                {
                    ManagementClass wmiClassAce = new ManagementClass("Win32_Ace");
                    try
                    {
                        using (ManagementClass managementClass2 = new ManagementClass("Win32_SecurityDescriptor"))
                        {
                            using (ManagementBaseObject methodParameters = managementClass1.GetMethodParameters("Create"))
                            {
                                ManagementObject[] managementObjectArray = (ManagementObject[])null;
                                try
                                {
                                    managementObjectArray = share.Aces == null ? new ManagementObject[0] : share.Aces.Select <Ace, ManagementObject>((Func <Ace, ManagementObject>)(a =>
                                    {
                                        if (a == null)
                                        {
                                            throw new NetworkShareAdapterException("Missing ACE information", 0);
                                        }
                                        NetworkShareAdapter._tracer.TraceInformation("Creating ACE for account {0}\\{1}", (object)a.domain, (object)(a.name ?? new SecurityIdentifier(a.sid, 0).ToString()));
                                        ManagementObject instance1 = wmiClassTrustee.CreateInstance();
                                        instance1["Domain"]        = (object)a.domain;
                                        instance1["Name"]          = (object)a.name;
                                        instance1["SID"]           = (object)a.sid;
                                        ManagementObject instance2 = wmiClassAce.CreateInstance();
                                        string index1 = "AccessMask";

                                        instance2[index1] = (ValueType)a.accessMask;
                                        string index2     = "AceFlags";

                                        instance2[index2] = (Enum)a.aceFlags;
                                        string index3     = "AceType";

                                        instance2[index3] = (Enum)a.aceType;
                                        string index4     = "Trustee";
                                        ManagementObject managementObject = instance1;
                                        instance2[index4] = (object)managementObject;
                                        return(instance2);
                                    })).ToArray <ManagementObject>();
                                    ManagementObject instance = managementClass2.CreateInstance();
                                    instance["ControlFlags"] = (object)4U;
                                    instance["DACL"]         = (object)managementObjectArray;
                                    methodParameters["Path"] = (object)share.Path.Replace('/', '\\');
                                    methodParameters["Name"] = (object)share.Name;
                                    methodParameters["Type"] = (object)share.TypeMask;
                                    int?maxUsers = share.MaxUsers;
                                    if (maxUsers.HasValue)
                                    {
                                        ManagementBaseObject managementBaseObject = methodParameters;
                                        string index = "MaximumAllowed";
                                        maxUsers = share.MaxUsers;
                                        managementBaseObject[index] = (ValueType)(uint)maxUsers.Value;
                                    }
                                    methodParameters["Access"] = (object)instance;
                                    using (ManagementBaseObject managementBaseObject = managementClass1.InvokeMethod("Create", methodParameters, (InvokeMethodOptions)null))
                                    {
                                        uint num = (uint)managementBaseObject.Properties["ReturnValue"].Value;
                                        if ((int)num != 0)
                                        {
                                            throw new NetworkShareAdapterException(string.Format("Error adding share {0}", (object)share.Name), (int)num);
                                        }
                                    }
                                }
                                finally
                                {
                                    if (managementObjectArray != null)
                                    {
                                        foreach (ManagementObject managementObject in managementObjectArray)
                                        {
                                            managementObject.Dispose();
                                        }
                                    }
                                }
                            }
                        }
                    }
                    finally
                    {
                        if (wmiClassAce != null)
                        {
                            wmiClassAce.Dispose();
                        }
                    }
                }
                finally
                {
                    if (wmiClassTrustee != null)
                    {
                        wmiClassTrustee.Dispose();
                    }
                }
            }
        }
Esempio n. 2
0
 public abstract void CreateFileShare(NetworkShareInfo share);