C#.Net Mac等のPC情報取得
6304 ワード
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";
}
}
}