91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ManagementClass類解析和C#如何獲取硬件的相關信息

發布時間:2020-06-28 20:09:15 來源:網絡 閱讀:1688 作者:彭澤0902 欄目:編程語言

   在.NET的項目中,有時候需要獲取計算機的硬件的相關信息,在C#語言中需要利用ManagementClass這個類來進行相關操作。

   現在先來介紹一下ManagementClass類,首先看一下類的繼承結構:

ManagementClass類解析和C#如何獲取硬件的相關信息

 

    現在看一下MSDN對ManagementClass類的解釋,對表示一個通用信息模型 (CIM) 管理類。 管理類是 WMI 類,如 Win32_LogicalDisk, ,該類型可表示一個磁盤驅動器,并 Win32_Process, ,它表示的進程 Notepad.exe 等。 此類的成員可以訪問 WMI 數據,使用一個特定的 WMI 類路徑。

  一. 接下來我們來看一下ManagementClass類中一些較為常用的方法的源碼:

  1.GetInstances():此方法存在四個重載

ManagementClass類解析和C#如何獲取硬件的相關信息 下面提供一下GetInstances(EnumerationOptions options)重載版本的代碼:

public ManagementObjectCollection GetInstances(EnumerationOptions options) 
         { 
            if ((null == Path) || (null == Path.Path) || (0 == Path.Path.Length))
                 throw new InvalidOperationException(); 

            Initialize ( false );
            IEnumWbemClassObject enumWbem = null;
 
            EnumerationOptions o = (null == options) ? new EnumerationOptions() : (EnumerationOptions)options.Clone();
             o.EnsureLocatable = false; o.PrototypeOnly = false;
 
             SecurityHandler securityHandler    = null;
            int status                        = (int)ManagementStatus.NoError;

             try 
            {
                securityHandler = Scope.GetSecurityHandler(); 
                            status = scope.GetSecuredIWbemServicesHandler(Scope.GetIWbemServices() ).CreateInstanceEnum_(ClassName, 
                                                            o.Flags,
                                                             o.GetContext(), 
                                                            ref enumWbem
                                                              );
             }
            finally 
             {
                if (securityHandler != null) 
                    securityHandler.Reset(); 
            }
 
             if (status < 0)
            {
                 if ((status & 0xfffff000) == 0x80041000)
                     ManagementException.ThrowWithExtendedInfo((ManagementStatus)status); 
                else
                     Marshal.ThrowExceptionForHR(status); 
            } 

            return new ManagementObjectCollection(Scope, o, enumWbem); 
        }

2.Get():

 ManagementClass類解析和C#如何獲取硬件的相關信息

  下面提供一下Get()重載版本的代碼:

public ManagementObjectCollection Get() 
        {
            Initialize (); 
            IEnumWbemClassObject ew = null; 
            SecurityHandler securityHandler = scope.GetSecurityHandler();
            EnumerationOptions enumOptions = (EnumerationOptions)options.Clone(); 

            int status = (int)ManagementStatus.NoError;

            try 
            {
                if ((query.GetType() == typeof(SelectQuery)) && 
                    (((SelectQuery)query).Condition == null) &&
                    (((SelectQuery)query).SelectedProperties == null) &&
                    (options.EnumerateDeep == true))
                { 
                    enumOptions.EnsureLocatable = false; enumOptions.PrototypeOnly = false; 

                    if (((SelectQuery)query).IsSchemaQuery == false) 
                    {
                        status = scope.GetSecuredIWbemServicesHandler( scope.GetIWbemServices()  ).CreateInstanceEnum_(
                            ((SelectQuery)query).ClassName,
                            enumOptions.Flags, 
                            enumOptions.GetContext(),
                            ref ew); 
                    } 
                    else
                    { 
                        status = scope.GetSecuredIWbemServicesHandler(scope.GetIWbemServices() ).CreateClassEnum_(((SelectQuery)query).ClassName,
                            enumOptions.Flags,
                            enumOptions.GetContext(),
                            ref ew ); 
                    }
                } 
                else
                {
                    enumOptions.EnumerateDeep = true;
                    status = scope.GetSecuredIWbemServicesHandler(scope.GetIWbemServices() ).ExecQuery_(
                        query.QueryLanguage,
                        query.QueryString, 
                        enumOptions.Flags,
                        enumOptions.GetContext(), 
                        ref ew ); 
                }
            } 
            catch (COMException e)
            {
                //
                ManagementException.ThrowWithExtendedInfo(e); 
            }
            finally 
            { 
                securityHandler.Reset();
            } 

            if ((status & 0xfffff000) == 0x80041000)
            {
                ManagementException.ThrowWithExtendedInfo((ManagementStatus)status); 
            }
            else if ((status & 0x80000000) != 0) 
            { 
                Marshal.ThrowExceptionForHR(status);
            } 
            return new ManagementObjectCollection(scope, options, ew); 
        }

3.GetSubclasses():此方法存在四個重載版本

ManagementClass類解析和C#如何獲取硬件的相關信息

 下面提供一下GetSubclasses(EnumerationOptions options)重載版本的代碼:

 

public ManagementObjectCollection GetSubclasses(EnumerationOptions options)
        { 
             if (null == Path) 
                throw new InvalidOperationException();
 
             Initialize ( false ) ;
             IEnumWbemClassObject enumWbem = null;
            EnumerationOptions o = (null == options) ? new EnumerationOptions() : (EnumerationOptions)options.Clone(); 
            o.EnsureLocatable = false; o.PrototypeOnly = false; 
            SecurityHandler securityHandler = null; 
             int status                        = (int)ManagementStatus.NoError;

            try
             { 
                 securityHandler = Scope.GetSecurityHandler();
                            status = scope.GetSecuredIWbemServicesHandler( Scope.GetIWbemServices() ).CreateClassEnum_(ClassName, 
                    o.Flags, 
                     o.GetContext(),
                    ref enumWbem); 
            }
            finally
             {
                if (securityHandler != null) 
                     securityHandler.Reset();
             } 
 
            if (status < 0)
             { 
                if ((status & 0xfffff000) == 0x80041000)
                    ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
                else
                     Marshal.ThrowExceptionForHR(status); 
            }
 
             return new ManagementObjectCollection(Scope, o, enumWbem); 
         }

 以上是對ManagementClass類的部分誰明,此類型的所有公共靜態成員都是線程安全的。不保證所有實例成員都是線程安全的。

二.ManagementClass類的部分應用:

 

  /// <summary>
        /// 獲得CPU編號
        /// </summary>
        /// <returns></returns>
        public string GetCpuid()
        {
            var cpuid = string.Empty;
            var mc = new ManagementClass("Win32_Processor");
            var moc = mc.GetInstances();
            foreach (var o in moc)
            {
                var mo = (ManagementObject) o;
                cpuid = mo.Properties["ProcessorId"].Value.ToString();
            }
            return cpuid;
        }

        /// <summary>
        /// 獲取硬盤序列號
        /// </summary>
        /// <returns></returns>
        public string GetDiskSerialNumber()
        {
            //這種模式在插入一個U盤后可能會有不同的結果,如插入我的手機時
            var hDid = string.Empty;
            var mc = new ManagementClass("Win32_DiskDrive");
            var moc = mc.GetInstances();
            foreach (var o in moc)
            {
                var mo = (ManagementObject) o;
                hDid = (string)mo.Properties["Model"].Value;
                //這名話解決有多個物理盤時產生的問題,只取第一個物理硬盤
                break;
            }
            return hDid;
        }

        /// <summary>
        /// 獲取網卡硬件地址
        /// </summary>
        /// <returns></returns> 
        public string GetMacAddress()
        {
            var mac = "";
            var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            var moc = mc.GetInstances();
            foreach (var o in moc)
            {
                var mo = (ManagementObject) o;
                if (!(bool) mo["IPEnabled"]) continue;
                mac = mo["MacAddress"].ToString();
                break;
            }
            return mac;
        }

        /// <summary>
        /// 獲取IP地址
        /// </summary>
        /// <returns></returns>
        public string GetIpAddress()
        {
            var st = string.Empty;
            var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            var moc = mc.GetInstances();
            foreach (var o in moc)
            {
                var mo = (ManagementObject) o;
                if (!(bool) mo["IPEnabled"]) continue;
                var ar = (Array)(mo.Properties["IpAddress"].Value);
                st = ar.GetValue(0).ToString();
                break;
            }
            return st;
        }

        /// <summary>
        /// 操作系統的登錄用戶名
        /// </summary>
        /// <returns></returns> 
        public string GetUserName()
        {
            return Environment.UserName;
        }


        /// <summary>
        /// 獲取計算機名
        /// </summary>
        /// <returns></returns>
        public string GetComputerName()
        {
            return Environment.MachineName;
        }

        /// <summary>
        /// 操作系統類型
        /// </summary>
        /// <returns></returns> 
        public string GetSystemType()
        {
            var st = string.Empty;
            var mc = new ManagementClass("Win32_ComputerSystem");
            var moc = mc.GetInstances();
            foreach (var o in moc)
            {
                var mo = (ManagementObject) o;
                st = mo["SystemType"].ToString();
            }
            return st;
        }

        /// <summary>
        /// 物理內存
        /// </summary>
        /// <returns></returns>
        public string GetPhysicalMemory()
        {
            var st = string.Empty;
            var mc = new ManagementClass("Win32_ComputerSystem");
            var moc = mc.GetInstances();
            foreach (var o in moc)
            {
                var mo = (ManagementObject) o;
                st = mo["TotalPhysicalMemory"].ToString();
            }
            return st;
        }

        /// <summary>
        /// 顯卡PNPDeviceID
        /// </summary>
        /// <returns></returns>
        public string GetVideoPnpid()
        {
            var st = "";
            var mos = new ManagementObjectSearcher("Select * from Win32_VideoController");
            foreach (var o in mos.Get())
            {
                var mo = (ManagementObject) o;
                st = mo["PNPDeviceID"].ToString();
            }
            return st;
        }

        /// <summary>
        /// 聲卡PNPDeviceID
        /// </summary>
        /// <returns></returns>
        public string GetSoundPnpid()
        {
            var st = string.Empty;
            var mos = new ManagementObjectSearcher("Select * from Win32_SoundDevice");
            foreach (var o in mos.Get())
            {
                var mo = (ManagementObject) o;
                st = mo["PNPDeviceID"].ToString();
            }
            return st;
        }

        /// <summary>
        /// CPU版本信息
        /// </summary>
        /// <returns></returns>
        public string GetCpuVersion()
        {
            var st = string.Empty;
            var mos = new ManagementObjectSearcher("Select * from Win32_Processor");
            foreach (var o in mos.Get())
            {
                var mo = (ManagementObject) o;
                st = mo["Version"].ToString();
            }
            return st;
        }

        /// <summary>
        /// CPU名稱信息
        /// </summary>
        /// <returns></returns>
        public string GetCpuName()
        {
            var st = string.Empty;
            var driveId = new ManagementObjectSearcher("Select * from Win32_Processor");
            foreach (var o in driveId.Get())
            {
                var mo = (ManagementObject) o;
                st = mo["Name"].ToString();
            }
            return st;
        }

        /// <summary>
        /// CPU制造廠商
        /// </summary>
        /// <returns></returns>
        public string GetCpuManufacturer()
        {
            var st = string.Empty;
            var mos = new ManagementObjectSearcher("Select * from Win32_Processor");
            foreach (var o in mos.Get())
            {
                var mo = (ManagementObject) o;
                st = mo["Manufacturer"].ToString();
            }
            return st;
        }

        /// <summary>
        /// 主板制造廠商
        /// </summary>
        /// <returns></returns>
        public string GetBoardManufacturer()
        {
            var query = new SelectQuery("Select * from Win32_BaseBoard");
            var mos = new ManagementObjectSearcher(query);
            var data = mos.Get().GetEnumerator();
            data.MoveNext();
            var board = data.Current;
            return board.GetPropertyValue("Manufacturer").ToString();
        }

        /// <summary>
        /// 主板編號
        /// </summary>
        /// <returns></returns>
        public string GetBoardId()
        {
            var st = string.Empty;
            var mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");
            foreach (var o in mos.Get())
            {
                var mo = (ManagementObject) o;
                st = mo["SerialNumber"].ToString();
            }
            return st;
        }

        /// <summary>
        /// 主板型號
        /// </summary>
        /// <returns></returns>
        public string GetBoardType()
        {
            var st = string.Empty;
            var mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");
            foreach (var o in mos.Get())
            {
                var mo = (ManagementObject) o;
                st = mo["Product"].ToString();
            }
            return st;
        }


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

秦安县| 丰城市| 岱山县| 霍城县| 桐乡市| 太仆寺旗| 开江县| 黑河市| 盐池县| 肥东县| 成都市| 贵州省| 古丈县| 靖远县| 临海市| 岱山县| 固安县| 青龙| 上高县| 新和县| 宁阳县| 三都| 呼和浩特市| 巴林左旗| 丹棱县| 和政县| 原阳县| 潼南县| 扎囊县| 贵南县| 静海县| 定州市| 平阳县| 大庆市| 满洲里市| 鹤壁市| 宣城市| 乐安县| 托克托县| 响水县| 阳城县|