c#中如何获取本机MAC地址、IP地址、硬盘ID、CPU序列号等系统信息
发布时间:2018-01-04 10:07:07 查看:2668
using System.Management;
-
-
-
public class GetSystemInfo
-
{
-
-
-
-
-
public static string GetUserName()
-
{
-
try
-
{
-
string strUserName = string.Empty;
-
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
-
ManagementObjectCollection moc = mc.GetInstances();
-
foreach (ManagementObject mo in moc)
-
{
-
strUserName = mo["UserName"].ToString();
-
}
-
moc = null;
-
mc = null;
-
return strUserName;
-
}
-
catch
-
{
-
return "unknown";
-
}
-
}
-
-
-
-
-
public static string GetMacAddress()
-
{
-
try
-
{
-
string strMac = string.Empty;
-
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
-
ManagementObjectCollection moc = mc.GetInstances();
-
foreach (ManagementObject mo in moc)
-
{
-
if ((bool)mo["IPEnabled"] == true)
-
{
-
strMac = mo["MacAddress"].ToString();
-
}
-
}
-
moc = null;
-
mc = null;
-
return strMac;
-
}
-
catch
-
{
-
return "unknown";
-
}
-
}
-
-
-
-
-
public static string getMacAddr_Local()
-
{
-
string madAddr = null;
-
try
-
{
-
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
-
ManagementObjectCollection moc2 = mc.GetInstances();
-
foreach (ManagementObject mo in moc2)
-
{
-
if (Convert.ToBoolean(mo["IPEnabled"]) == true)
-
{
-
madAddr = mo["MacAddress"].ToString();
-
madAddr = madAddr.Replace(':', '-');
-
}
-
mo.Dispose();
-
}
-
if (madAddr == null)
-
{
-
return "unknown";
-
}
-
else
-
{
-
return madAddr;
-
}
-
}
-
catch (Exception)
-
{
-
return "unknown";
-
}
-
}
-
-
-
-
-
public static string GetClientLocalIPv6Address()
-
{
-
string strLocalIP = string.Empty;
-
try
-
{
-
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
-
IPAddress ipAddress = ipHost.AddressList[0];
-
strLocalIP = ipAddress.ToString();
-
return strLocalIP;
-
}
-
catch
-
{
-
return "unknown";
-
}
-
}
-
-
-
-
-
public static string GetClientLocalIPv4Address()
-
{
-
string strLocalIP = string.Empty;
-
try
-
{
-
IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
-
IPAddress ipAddress = ipHost.AddressList[0];
-
strLocalIP = ipAddress.ToString();
-
return strLocalIP;
-
}
-
catch
-
{
-
return "unknown";
-
}
-
}
-
-
-
-
-
public static List<string> GetClientLocalIPv4AddressList()
-
{
-
List<string> ipAddressList = new List<string>();
-
try
-
{
-
IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
-
foreach (IPAddress ipAddress in ipHost.AddressList)
-
{
-
if (!ipAddressList.Contains(ipAddress.ToString()))
-
{
-
ipAddressList.Add(ipAddress.ToString());
-
}
-
}
-
}
-
catch
-
{
-
-
}
-
return ipAddressList;
-
}
-
-
-
-
-
-
public static string GetClientInternetIPAddress()
-
{
-
string strInternetIPAddress = string.Empty;
-
try
-
{
-
using (WebClient webClient = new WebClient())
-
{
-
strInternetIPAddress = webClient.DownloadString("http://www.coridc.com/ip");
-
Regex r = new Regex("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}");
-
Match mth = r.Match(strInternetIPAddress);
-
if (!mth.Success)
-
{
-
strInternetIPAddress = GetClientInternetIPAddress2();
-
mth = r.Match(strInternetIPAddress);
-
if (!mth.Success)
-
{
-
strInternetIPAddress = "unknown";
-
}
-
}
-
return strInternetIPAddress;
-
}
-
}
-
catch
-
{
-
return "unknown";
-
}
-
}
-
-
-
-
-
private static string GetClientInternetIPAddress2()
-
{
-
string tempip = "";
-
try
-
{
-
-
WebRequest wr = WebRequest.Create("http://iframe.ip138.com/ic.asp");
-
Stream s = wr.GetResponse().GetResponseStream();
-
StreamReader sr = new StreamReader(s, Encoding.Default);
-
string all = sr.ReadToEnd();
-
-
int start = all.IndexOf("[") + 1;
-
int end = all.IndexOf("]", start);
-
tempip = all.Substring(start, end - start);
-
sr.Close();
-
s.Close();
-
return tempip;
-
}
-
catch
-
{
-
return "unknown";
-
}
-
}
-
-
-
-
-
public static string GetDiskID()
-
{
-
try
-
{
-
string strDiskID = string.Empty;
-
ManagementClass mc = new ManagementClass("Win32_DiskDrive");
-
ManagementObjectCollection moc = mc.GetInstances();
-
foreach (ManagementObject mo in moc)
-
{
-
strDiskID = mo.Properties["Model"].Value.ToString();
-
}
-
moc = null;
-
mc = null;
-
return strDiskID;
-
}
-
catch
-
{
-
return "unknown";
-
}
-
}
-
-
-
-
-
public static string GetCpuID()
-
{
-
try
-
{
-
string strCpuID = string.Empty;
-
ManagementClass mc = new ManagementClass("Win32_Processor");
-
ManagementObjectCollection moc = mc.GetInstances();
-
foreach (ManagementObject mo in moc)
-
{
-
strCpuID = mo.Properties["ProcessorId"].Value.ToString();
-
}
-
moc = null;
-
mc = null;
-
return strCpuID;
-
}
-
catch
-
{
-
return "unknown";
-
}
-
}
-
-
-
-
-
public static string GetSystemType()
-
{
-
try
-
{
-
string strSystemType = string.Empty;
-
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
-
ManagementObjectCollection moc = mc.GetInstances();
-
foreach (ManagementObject mo in moc)
-
{
-
strSystemType = mo["SystemType"].ToString();
-
}
-
moc = null;
-
mc = null;
-
return strSystemType;
-
}
-
catch
-
{
-
return "unknown";
-
}
-
}
-
-
-
-
-
public static string GetSystemName()
-
{
-
try
-
{
-
string strSystemName = string.Empty;
-
ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT PartComponent FROM Win32_SystemOperatingSystem");
-
foreach (ManagementObject mo in mos.Get())
-
{
-
strSystemName = mo["PartComponent"].ToString();
-
}
-
mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT Caption FROM Win32_OperatingSystem");
-
foreach (ManagementObject mo in mos.Get())
-
{
-
strSystemName = mo["Caption"].ToString();
-
}
-
return strSystemName;
-
}
-
catch
-
{
-
return "unknown";
-
}
-
}
-
-
-
-
-
public static string GetTotalPhysicalMemory()
-
{
-
try
-
{
-
string strTotalPhysicalMemory = string.Empty;
-
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
-
ManagementObjectCollection moc = mc.GetInstances();
-
foreach (ManagementObject mo in moc)
-
{
-
strTotalPhysicalMemory = mo["TotalPhysicalMemory"].ToString();
-
}
-
moc = null;
-
mc = null;
-
return strTotalPhysicalMemory;
-
}
-
catch
-
{
-
return "unknown";
-
}
-
}
-
-
-
-
-
-
public static string GetMotherBoardID()
-
{
-
try
-
{
-
ManagementClass mc = new ManagementClass("Win32_BaseBoard");
-
ManagementObjectCollection moc = mc.GetInstances();
-
string strID = null;
-
foreach (ManagementObject mo in moc)
-
{
-
strID = mo.Properties["SerialNumber"].Value.ToString();
-
break;
-
}
-
return strID;
-
}
-
catch
-
{
-
return "unknown";
-
}
-
}
-
-
-
-
-
-
public static string GetAllUsersDesktopFolderPath()
-
{
-
RegistryKey folders;
-
folders = OpenRegistryPath(Registry.LocalMachine, @"/software/microsoft/windows/currentversion/explorer/shell folders");
-
string desktopPath = folders.GetValue("Common Desktop").ToString();
-
return desktopPath;
-
}
-
-
-
-
public static string GetAllUsersStartupFolderPath()
-
{
-
RegistryKey folders;
-
folders = OpenRegistryPath(Registry.LocalMachine, @"/software/microsoft/windows/currentversion/explorer/shell folders");
-
string Startup = folders.GetValue("Common Startup").ToString();
-
return Startup;
-
}
-
private static RegistryKey OpenRegistryPath(RegistryKey root, string s)
-
{
-
s = s.Remove(0, 1) + @"/";
-
while (s.IndexOf(@"/") != -1)
-
{
-
root = root.OpenSubKey(s.Substring(0, s.IndexOf(@"/")));
-
s = s.Remove(0, s.IndexOf(@"/") + 1);
-
}
-
return root;
-
}
-
-
-
private void Test()
-
{
-
RegistryKey folders;
-
folders = OpenRegistryPath(Registry.LocalMachine, @"/software/microsoft/windows/currentversion/explorer/shell folders");
-
-
string desktopPath = folders.GetValue("Common Desktop").ToString();
-
-
string fontsPath = folders.GetValue("Fonts").ToString();
-
-
string nethoodPath = folders.GetValue("Nethood").ToString();
-
-
string personalPath = folders.GetValue("Personal").ToString();
-
-
string programsPath = folders.GetValue("Programs").ToString();
-
-
string recentPath = folders.GetValue("Recent").ToString();
-
-
string sendtoPath = folders.GetValue("Sendto").ToString();
-
-
string startmenuPath = folders.GetValue("Startmenu").ToString();
-
-
string startupPath = folders.GetValue("Startup").ToString();
-
-
string favoritesPath = folders.GetValue("Favorites").ToString();
-
-
string historyPath = folders.GetValue("History").ToString();
-
-
string cookiesPath = folders.GetValue("Cookies").ToString();
-
-
string cachePath = folders.GetValue("Cache").ToString();
-
-
string appdataPath = folders.GetValue("Appdata").ToString();
-
-
string printhoodPath = folders.GetValue("Printhood").ToString();
-
}</span>