C#.Net获取Mac等PC信息

时间:2023-12-25 17:53:43
using System.Management;
using System.Net;
    public class PCInfoHelper
{
/// <summary>
/// 操作系统的登录用户名
/// </summary>
/// <returns></returns>
public static string GetUserName()
{
try
{
string username = "";
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
username = mo["UserName"].ToString();
}
return username;
}
catch
{
return "unknow";
}
} /// <summary>
/// 获取MAC地址
/// </summary>
/// <returns></returns>
public static string GetMacAddress()
{
try
{
string mac = "";
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
mac = mo["MacAddress"].ToString();
break;
}
}
return mac;
}
catch
{
return "unknow";
}
} /// <summary>
/// 获取硬盘ID
/// </summary>
/// <returns></returns>
public static string GetDiskID()
{
try
{
String diskid = "";
ManagementClass mc = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
diskid = (String)mo.Properties["Model"].Value.ToString();
}
return diskid;
}
catch
{
return "unknow";
}
} /// <summary>
/// 获取CPU ID
/// </summary>
/// <returns></returns>
public static string GetCpuID()
{
try
{
string cpuid = "";
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
cpuid = mo.Properties["ProcessorId"].Value.ToString();
}
return cpuid;
}
catch
{
return "unknow";
}
} /// <summary>
/// 系统名称
/// </summary>
/// <returns></returns>
public static string GetSystemType()
{
try
{
string systype = "";
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
systype = mo["SystemType"].ToString();
}
return systype;
}
catch
{
return "unknow";
}
} /// <summary>
/// 获取物理内存
/// </summary>
/// <returns></returns>
public static string GetTotalPhysicalMemory()
{
try
{
string phymemory = "";
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
phymemory = mo["TotalPhysicalMemory"].ToString();
}
return phymemory;
}
catch
{
return "unknow";
}
} /// <summary>
/// 获取客户端内网IP地址
/// </summary>
/// <returns></returns>
public static string GetLocalIPAddress()
{
try
{
string localIP = null;
IPHostEntry ipHost = System.Net.Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipaddress = ipHost.AddressList[0];
localIP = ipaddress.ToString();
return "内网IP地址:" + localIP;
}
catch
{
return "内网IP地址:unknown";
}
} /// <summary>
/// 获得客户端外网IP地址
/// </summary>
/// <returns>IP地址</returns>
public static string GetInternetIPAddress()
{
try
{
string internetIP = "";
using (WebClient webClient = new WebClient())
{
internetIP = webClient.DownloadString("http://www.coridc.com/ip");//从外部网页获得IP地址
//判断IP是否合法
if (!System.Text.RegularExpressions.Regex.IsMatch(internetIP, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
{
internetIP = webClient.DownloadString("http://fw.qq.com/ipaddress");//从腾讯提供的API中获得IP地址
}
}
return "外网IP地址:" + internetIP;
}
catch
{
return "外网IP地址:unknown";
}
}
}