1 //取机器名 2 public string GetHostName() 3 { 4 return System.Net.Dns.GetHostName(); 5 } 6 7 //取CPU编号 8 public String GetCpuID() 9 { 10 try 11 { 12 ManagementClass mc = new ManagementClass("Win32_Processor"); 13 ManagementObjectCollection moc = mc.GetInstances(); 14 15 String strCpuID = null; 16 foreach (ManagementObject mo in moc) 17 { 18 strCpuID = mo.Properties["ProcessorId"].Value.ToString(); 19 break; 20 } 21 return strCpuID; 22 } 23 catch 24 { 25 return ""; 26 } 27 28 } 29 30 //取第一块硬盘编号 31 public String GetHardDiskID() 32 { 33 try 34 { 35 ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia"); 36 String strHardDiskID = null; 37 foreach (ManagementObject mo in searcher.Get()) 38 { 39 strHardDiskID = mo["SerialNumber"].ToString().Trim(); 40 break; 41 } 42 return strHardDiskID; 43 } 44 catch 45 { 46 return ""; 47 } 48 } 49 50 51 //获取MAC地址 52 public string GetMacAddress() 53 { 54 string strMacAddress = ""; 55 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 56 ManagementObjectCollection moc = mc.GetInstances(); 57 foreach (ManagementObject mo in moc) 58 { 59 if ((bool)mo["IPEnabled"] == true) 60 { 61 strMacAddress += mo["MacAddress"].ToString(); 62 } 63 } 64 return strMacAddress; 65 66 } 67 68 69 #region GetCPUName 70 //取得CPU名 71 /// <summary> 72 /// 取得CPU名 73 /// </summary> 74 /// <returns>CPU名(例:Intel(R) Xeon(R) CPU X3210 @ 2.13GHz)</returns> 75 public string GetCPUName() 76 { 77 try 78 { 79 ManagementClass managementClass = new ManagementClass("Win32_Processor"); 80 ManagementObjectCollection managementObj = managementClass.GetInstances(); 81 foreach (ManagementObject mo in managementObj) 82 { 83 return mo["Name"].ToString(); 84 } 85 return "Unknown."; 86 } 87 catch 88 { 89 return "Unknown."; 90 } 91 } 92 93 #endregion 94 95 #region GetTotalMemorySize 96 97 /// <summary> 98 /// 内存大小 99 /// </summary> 100 /// <returns>内存大小(KB)</returns> 101 public string GetTotalMemorySize() 102 { 103 try 104 { 105 ManagementClass managementClass = new ManagementClass("Win32_OperatingSystem"); 106 ManagementObjectCollection managementObj = managementClass.GetInstances(); 107 foreach (ManagementObject mo in managementObj) 108 { 109 return mo["TotalVisibleMemorySize"].ToString() + "KB"; 110 } 111 return "Unknown."; 112 } 113 catch 114 { 115 return "Unknown."; 116 } 117 } 118 119 #endregion 120 121 #region GetFreeDiscSpace 122 123 /// <summary> 124 /// 指定盘的剩余容量 125 /// </summary> 126 /// <returns>指定盘的剩余容量(MB)</returns> 127 public string GetFreeDiscSpace(string aDrive) 128 { 129 try 130 { 131 System.Diagnostics.PerformanceCounter pc = new System.Diagnostics.PerformanceCounter("LogicalDisk", "Free Megabytes", aDrive + ":"); 132 return pc.NextValue().ToString() + "MB"; 133 } 134 catch 135 { 136 return "Unknown."; 137 } 138 } 139 140 #endregion 141 142 #region GetIPAddress 143 144 /// <summary> 145 /// IP地址。 146 /// 多个的话,返回第一个 147 /// </summary> 148 /// <returns>IP地址</returns> 149 /// <remarks> 150 /// 没取到的时候返回127.0.0.1 151 /// </remarks> 152 public string GetIPAddress() 153 { 154 string[] ips = GetAllIPAddress(); 155 if (ips.Length > 0) 156 { 157 return ips[0]; 158 } 159 else 160 { 161 return "127.0.0.1"; 162 } 163 } 164 165 #endregion 166 167 #region GetAllIPAddress 168 169 /// <summary> 170 /// IP地址。 171 /// </summary> 172 /// <returns>IP地址数组</returns> 173 /// <remarks> 174 /// 没取到的时候返回长度为0的数组 175 /// </remarks> 176 public string[] GetAllIPAddress() 177 { 178 ArrayList list = new ArrayList(); 179 string[] ips = new string[0]; 180 181 try 182 { 183 System.Management.ManagementScope scope = new System.Management.ManagementScope("root\\cimv2"); 184 scope.Connect(); 185 System.Management.ObjectQuery query 186 = new System.Management.ObjectQuery("Select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE"); 187 System.Management.ManagementObjectSearcher searcher 188 = new System.Management.ManagementObjectSearcher(scope, query); 189 System.Management.ManagementObjectCollection collection = searcher.Get(); 190 191 foreach (System.Management.ManagementObject obj in collection) 192 { 193 foreach (string ip in (string[])obj.Properties["IPAddress"].Value) 194 { 195 list.Add(ip); 196 } 197 } 198 199 ips = (string[])list.ToArray(typeof(string)); 200 } 201 catch (System.Exception e) 202 { 203 204 } 205 206 return ips; 207 } 208 209 #endregion