コード例 #1
0
        private static List <LanManShareDetails> GetLanManShares()
        {
            List <LanManShareDetails> shares   = new List <LanManShareDetails>();
            ManagementClass           objClass = new ManagementClass("Win32_Share");

            foreach (ManagementObject objShare in objClass.GetInstances())
            {
                try
                {
                    PropertyData       ong          = objShare.Properties["MaximumAllowed"];
                    LanManShareDetails shareDetails = new LanManShareDetails
                    {
                        MaxConnectionsNum = (ong.Value == null) ? UInt32.MaxValue : Convert.ToUInt32(ong.Value),
                        Name        = Convert.ToString(objShare.Properties["Name"].Value),
                        Path        = Convert.ToString(objShare.Properties["Path"].Value),
                        Description =
                            Convert.ToString(objShare.Properties["Description"].Value)
                    };
                    shares.Add(shareDetails);
                }
                catch
                {
                }
            }
            return(shares);
        }
コード例 #2
0
        // http://msdn.microsoft.com/en-us/library/aa389393%28VS.85%29.aspx
        public static void SetLanManShare(LanManShareDetails share)
        {
            try
            {
                ManagementObject shareMO = new ManagementObject("Win32_Share=" + "'" + share.Name + "'");
                if (shareMO != null)
                {
                    Log.Warn("[{0}] may already be shared.", share.Name);
                    ManagementBaseObject inDeleteParams  = null;
                    ManagementBaseObject outDeleteParams = shareMO.InvokeMethod("Delete", inDeleteParams, null);
                    if (outDeleteParams != null)
                    {
                        Log.Warn("Attempt to remove share returned [{0}]",
                                 Convert.ToUInt32(outDeleteParams.Properties["ReturnValue"].Value));
                    }
                    else
                    {
                        Log.Error("Attempt to remove share returned null!");
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WarnException("Sharing may not work if this is not \"Not Found\"", ex);
            }
            ManagementClass managementClass = new ManagementClass("Win32_Share");

            ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");

            object[] userobj = new object[share.UserAccessRules.Count];
            for (int index = 0; index < share.UserAccessRules.Count; index++)
            {
                UserAccessRuleExport userAccessRule = share.UserAccessRules[index];
                ManagementObject     mUser          = new ManagementClass(new ManagementPath("Win32_Trustee"), null);
                string domainName = string.Empty;
                string userName   = string.Empty;
                SplitUserName(userAccessRule.DomainUserIdentity, ref domainName, ref userName);

                NTAccount          ntAccountUser = new NTAccount(domainName, userName);
                SecurityIdentifier sidUser       = (SecurityIdentifier)ntAccountUser.Translate(typeof(SecurityIdentifier));
                byte[]             sidArrayUser  = new byte[sidUser.BinaryLength];
                sidUser.GetBinaryForm(sidArrayUser, 0);
                mUser["SID"] = sidArrayUser;

                ManagementObject mACEUser = new ManagementClass(new ManagementPath("Win32_Ace"), null);
                mACEUser["AccessMask"] = userAccessRule.AccessMask;
                mACEUser["AceFlags"]   = userAccessRule.InheritanceFlags;
                mACEUser["AceType"]    = userAccessRule.Type;
                mACEUser["Trustee"]    = mUser;

                userobj[index] = mACEUser;
            }

            ManagementObject secDescriptor = new ManagementClass(new ManagementPath("Win32_SecurityDescriptor"), null);

            // securityDescriptorDefault.Properties["ControlFlags"].Value = 0x8; // Indicates an SD with a default DACL. the object receives the default DACL from the access token of the creator
            secDescriptor["ControlFlags"] = 4 + 256; //
            secDescriptor["DACL"]         = userobj;

            inParams["Description"]    = share.Description;
            inParams["Access"]         = secDescriptor;
            inParams["Name"]           = share.Name;
            inParams["Path"]           = share.Path;
            inParams["MaximumAllowed"] = share.MaxConnectionsNum;
            inParams["Type"]           = 0x0;

            // Invoke the method on the ManagementClass object
            ManagementBaseObject outParams = managementClass.InvokeMethod("Create", inParams, null);

            // Check to see if the method invocation was successful
            string exceptionText = String.Empty;

            if (outParams != null)
            {
                UInt32 ret = Convert.ToUInt32(outParams.Properties["ReturnValue"].Value);

                switch (ret)
                {
                case 0: // Success
                    break;

                case 2:
                    exceptionText = "Access Denied";
                    break;

                default:
                    //case 8:
                    exceptionText = String.Format("Unknown Failure [{0}]", ret);
                    break;

                case 9:
                    exceptionText = "Invalid Name";
                    break;

                case 10:
                    exceptionText = "Invalid Level";
                    break;

                case 21:
                    exceptionText = "Invalid Parameter";
                    break;

                case 22:
                    // This one is okay as we are recreating the share !
                    // exceptionText = "Duplicate Share";
                    break;

                case 23:
                    exceptionText = "Redirected Path";
                    break;

                case 24:
                    exceptionText = "Unknown Device or Directory";
                    break;

                case 25:
                    exceptionText = "Net Name Not Found";
                    break;
                }
            }
            else
            {
                exceptionText = "Unable to get return state";
            }

            if (!String.IsNullOrEmpty(exceptionText))
            {
                throw new Exception(exceptionText);
            }
        }