/// <summary> /// 获取本机的MAC地址 /// </summary> /// <returns></returns> public static string GetMACString() { ManagementClass mAdapter = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection mo = mAdapter.GetInstances(); foreach (ManagementBaseObject m in mo) { if ((bool)m["IpEnabled"] == true) { return m["MacAddress"].ToString(); } } mo.Dispose(); return null; //ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); //ManagementObjectCollection moc = mc.GetInstances(); //foreach (ManagementObject mo in moc) //{ // //如果没有启用IP设置的网络设备则跳过 // if (!(bool)mo["IPEnabled"]) // continue; // // 获取属性 // string caption = mo["Caption"].ToString(); //名称 // string[] addresses = (string[])mo["IPAddress"]; //IP地址 // string[] subnets = (string[])mo["IPSubnet"]; //子网掩码 // string[] gateways = (string[])mo["DefaultIPGateway"]; //网关 // string[] dnses = (string[])mo["DNSServerSearchOrder"]; //DNS // ManagementBaseObject newIP; // // 修改IP及子网掩码 // newIP = mo.GetMethodParameters("EnableStatic"); // newIP["IPAddress"] = new string[] { "192.168.2.51" }; // newIP["SubnetMask"] = new string[] { "255.255.255.0" }; //修改IP同时,需要填写子网掩码 // mo.InvokeMethod("EnableStatic", newIP, null); // // 设置网关地址 // newIP = mo.GetMethodParameters("SetGateways"); // newIP["DefaultIPGateway"] = new string[] { "192.168.2.1" }; // mo.InvokeMethod("SetGateways", newIP, null); // // 设置DNS // newIP = mo.GetMethodParameters("SetDNSServerSearchOrder"); // newIP["DNSServerSearchOrder"] = new string[] { "192.168.1.10" }; // mo.InvokeMethod("SetDNSServerSearchOrder", newIP, null); } /// <summary> /// 获取CPU的个数 /// </summary> /// <returns></returns> public static int GetCpuCount() { try { using (ManagementClass mCpu = new ManagementClass("Win32_Processor")) { ManagementObjectCollection cpus = mCpu.GetInstances(); return cpus.Count; } } catch { } return -1; } /// <summary> /// 获取CPU的频率这里之所以使用string类型的数组,主要是因为cpu的多核 /// </summary> /// <returns></returns> public static string[] GetCpuMHZ() { ManagementClass mc = new ManagementClass("Win32_Processor"); ManagementObjectCollection cpus = mc.GetInstances(); string[] mHz = new string[cpus.Count]; int c = 0; ManagementObjectSearcher mySearch = new ManagementObjectSearcher("select * from Win32_Processor"); foreach (ManagementObject mo in mySearch.Get()) { mHz[c] = mo.Properties["CurrentClockSpeed"].Value.ToString(); c++; } mc.Dispose(); mySearch.Dispose(); return mHz; } /// <summary> /// 获取硬盘的大小 /// </summary> /// <returns></returns> public static string GetSizeOfDisk() { ManagementClass mc = new ManagementClass("Win32_DiskDrive"); ManagementObjectCollection moj = mc.GetInstances(); foreach (ManagementObject m in moj) { return m.Properties["Size"].Value.ToString(); } return "-1"; } /// <summary> /// 获取内存的大小 /// </summary> /// <returns></returns> public static string GetSizeOfMemery() { ManagementClass mc = new ManagementClass("Win32_OperatingSystem"); ManagementObjectCollection moc = mc.GetInstances(); double sizeAll = 0.0; foreach (ManagementObject m in moc) { if (m.Properties["TotalVisibleMemorySize"].Value != null) { sizeAll += Convert.ToDouble(m.Properties["TotalVisibleMemorySize"].Value.ToString()); } } mc = null; moc.Dispose(); return sizeAll.ToString(); }
时间: 2024-10-08 07:43:38