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

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

 

PS:网络转载

posted @ 2009-12-03 12:28  风w  阅读(1593)  评论(0编辑  收藏  举报