博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
winform取CPU编号、MAC地址、硬盘信息、IP地址、串口信息
阅读量:6262 次
发布时间:2019-06-22

本文共 3298 字,大约阅读时间需要 10 分钟。

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

转载于:https://www.cnblogs.com/xingvskong11/archive/2013/01/14/CPU-MAC-IP.html

你可能感兴趣的文章
《SolidWorks 2012中文版从入门到精通》一6.5 综合实例——斜齿圆柱齿轮
查看>>
storm集群的监控
查看>>
RHCE 6.0学习笔记-2 RHEL 6 使用光盘配置本地YUM源
查看>>
Mongodb定期备份
查看>>
Confluence 6 数据库设置
查看>>
刨根问底-struts-怎么加载配置的相应的信息
查看>>
解决mysql数据库大小写敏感问题
查看>>
jsp页面组成
查看>>
LCS记录
查看>>
C++开源跨平台类库集
查看>>
everything搜索工具小技巧
查看>>
一个 Sql语句优化的问题- STATISTICS 统计信息
查看>>
你不知道的KVO的内部实现
查看>>
转】MyEclipse10安装Log4E插件
查看>>
windows server2012r2 安装NET Framework 3.5
查看>>
[osg][osgEarth][原]基于OE自定义自由飞行漫游器(初级版)
查看>>
Java遇见HTML——JSP篇之JSP基础语法
查看>>
导出一个数据库中的表中的某一条数据
查看>>
JQuery初体验
查看>>
全球顶级黑客对决AI GeekPwn2017黑客大赛看点全面曝光
查看>>