日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

winform取CPU编号、MAC地址、硬盘信息、IP地址、串口信息

發布時間:2025/3/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 winform取CPU编号、MAC地址、硬盘信息、IP地址、串口信息 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

winform取CPU編號、MAC地址、硬盤信息、IP地址、串口信息

http://www.cnblogs.com/ccczqh/archive/2011/03/28/1997480.html
作者:ccczqh??來源:博客園??發布時間:2011-03-28 10:07??閱讀:91 次??原文鏈接???[收藏]?? winform取CPU編號、MAC地址、硬盤信息、IP地址、串口信息 2009年07月30日 星期四 13:54

using System.Management; //需要在解決方案中引用System.Management.DLL文件

??????? /// <summary>
??????? ///?取CPU編號
??????? /// </summary>
??????? /// <returns></returns>
??????? public static string GetCpuID()
??????? {
??????????? try
??????????? {
??????????????? ManagementClass mc = new ManagementClass("Win32_Processor");
??????????????? ManagementObjectCollection moc = mc.GetInstances();
??????????????? string strCpuID = null;
??????????????? foreach (ManagementObject mo in moc)
??????????????? {
??????????????????? strCpuID = mo.Properties["ProcessorId"].Value.ToString();
??????????????????? break;
??????????????? }
??????????????? return strCpuID;
??????????? }
??????????? catch
??????????? {
??????????????? return "";
??????????? }
??????? }

??????? /// <summary>
??????? ///?取第一塊硬盤編號
??????? /// </summary>
??????? /// <returns></returns>
??????? public static string GetHardDiskID()
??????? {
??????????? try
??????????? {
??????????????? ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
??????????????? string strHardDiskID = null;
??????????????? foreach (ManagementObject mo in searcher.Get())
??????????????? {
??????????????????? strHardDiskID = mo["SerialNumber"].ToString().Trim();
??????????????????? break;
??????????????? }
??????????????? return strHardDiskID;
??????????? }
??????????? catch
??????????? {
??????????????? return "";
??????????? }
??????? }

??????? /// <summary>
??????? ///?獲取網卡MAC地址
??????? /// </summary>
??????? /// <returns></returns>
??????? public static string GetNetCardMAC()
??????? {
??????????? try
??????????? {
??????????????? string stringMAC = "";
??????????????? ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
??????????????? ManagementObjectCollection MOC = MC.GetInstances();
??????????????? foreach (ManagementObject MO in MOC)
??????????????? {
??????????????????? if ((bool)MO["IPEnabled"] == true)
??????????????????? {
??????????????????????? stringMAC += MO["MACAddress"].ToString();

??????????????????? }
??????????????? }
??????????????? return stringMAC;
??????????? }
??????????? catch
??????????? {
??????????????? return "";
??????????? }
??????? }

??????? /// <summary>
??????? ///?獲取硬盤信息的代碼
??????? /// </summary>
??????? /// <param name="drvID"></param>
??????? /// <returns></returns>
??????? public static string GetVolOf(string drvID)
??????? {
??????????? try
??????????? {
??????????????? const int MAX_FILENAME_LEN = 256;
??????????????? int retVal = 0;
??????????????? int a = 0;
??????????????? int b = 0;
??????????????? string str1 = null;
??????????????? string str2 = null;
??????????????? int i = GetVolumeInformation(drvID + @":\", str1, MAX_FILENAME_LEN, ref retVal, a, b, str2, MAX_FILENAME_LEN);
??????????????? return retVal.ToString("x");
??????????? }
??????????? catch
??????????? {
??????????????? return "";
??????????? }
??????? }

??????? /// <summary>
??????? ///?獲取當前網卡IP地址
??????? /// </summary>
??????? /// <returns></returns>
??????? public static string GetNetCardIP()
??????? {
??????????? try
??????????? {
??????????????? string stringIP = "";
??????????????? ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
??????????????? ManagementObjectCollection MOC = MC.GetInstances();
??????????????? foreach (ManagementObject MO in MOC)
??????????????? {
??????????????????? if ((bool)MO["IPEnabled"] == true)
??????????????????? {
??????????????????????? string[] IPAddresses = (string[])MO["IPAddress"];
??????????????????????? if (IPAddresses.Length > 0)
??????????????????????? {
??????????????????????????? stringIP = IPAddresses[0].ToString();
??????????????????????? }
??????????????????? }
??????????????? }
??????????????? return stringIP;
??????????? }
??????????? catch
??????????? {
??????????????? return "";
??????????? }
??????? }

??????? #region?調用注冊表返回本地串口
??????? /// <summary>
??????? /// 串口函數(方法需調用注冊表,串口編程所用類)
??????? /// </summary>
??????? /// 使用命名空間:
??????? /// using System.Security;
??????? /// using System.Security.Permissions;
??????? /// <returns>返回此計算機串口數組</returns>
??????? public static string[] GetPortNames()//
??????? {
??????????? RegistryKey localMachine = null;
??????????? RegistryKey key2 = null;
??????????? string[] textArray = null;//這里有個判斷,判斷該注冊表項是否存在????
??????????? new RegistryPermission(RegistryPermissionAccess.Read, @"HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM").Assert();
??????????? try
??????????? {
??????????????? localMachine = Registry.LocalMachine;
??????????????? key2 = localMachine.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM", false);
??????????????? if (key2 != null)
??????????????? {
??????????????????? string[] valueNames = key2.GetValueNames();
??????????????????? textArray = new string[valueNames.Length];
??????????????????? for (int i = 0; i < valueNames.Length; i++)
??????????????????? {
??????????????????????? textArray[i] = (string)key2.GetValue(valueNames[i]);
??????????????????? }
??????????????? }
??????????? }
??????????? finally
??????????? {
??????????????? if (localMachine != null)
??????????????? {
??????????????????? localMachine.Close();
??????????????? } if (key2 != null)
??????????????? {
??????????????????? key2.Close();
??????????????? }
??????????????? CodeAccessPermission.RevertAssert();
??????????? } if (textArray == null)
??????????? {
??????????????? textArray = new string[0];
??????????? }
??????????? return textArray;
??????? }
??????? #endregion

總結

以上是生活随笔為你收集整理的winform取CPU编号、MAC地址、硬盘信息、IP地址、串口信息的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。