winform取CPU编号、MAC地址、硬盘信息、IP地址、串口信息
winform取CPU編號(hào)、MAC地址、硬盤(pán)信息、IP地址、串口信息
http://www.cnblogs.com/ccczqh/archive/2011/03/28/1997480.html作者:ccczqh??來(lái)源:博客園??發(fā)布時(shí)間:2011-03-28 10:07??閱讀:91 次??原文鏈接???[收藏]?? winform取CPU編號(hào)、MAC地址、硬盤(pán)信息、IP地址、串口信息 2009年07月30日 星期四 13:54
using System.Management; //需要在解決方案中引用System.Management.DLL文件
??????? /// <summary>
??????? ///?取CPU編號(hào)
??????? /// </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>
??????? ///?取第一塊硬盤(pán)編號(hào)
??????? /// </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>
??????? ///?獲取網(wǎng)卡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>
??????? ///?獲取硬盤(pán)信息的代碼
??????? /// </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>
??????? ///?獲取當(dāng)前網(wǎng)卡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?調(diào)用注冊(cè)表返回本地串口
??????? /// <summary>
??????? /// 串口函數(shù)(方法需調(diào)用注冊(cè)表,串口編程所用類(lèi))
??????? /// </summary>
??????? /// 使用命名空間:
??????? /// using System.Security;
??????? /// using System.Security.Permissions;
??????? /// <returns>返回此計(jì)算機(jī)串口數(shù)組</returns>
??????? public static string[] GetPortNames()//
??????? {
??????????? RegistryKey localMachine = null;
??????????? RegistryKey key2 = null;
??????????? string[] textArray = null;//這里有個(gè)判斷,判斷該注冊(cè)表項(xiàng)是否存在????
??????????? 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
總結(jié)
以上是生活随笔為你收集整理的winform取CPU编号、MAC地址、硬盘信息、IP地址、串口信息的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: include(),include_on
- 下一篇: PHP获取IP地址所在的地理位置