C# 我常用的获取计算机信息类

发布时间:2009年06月18日      浏览次数:574 次
using System;
using System.Management;
namespace MyData.Inc
{
/// <summary>
/// 计算机信息类
/// </summary>
public class Computer
{
#region 获取硬盘相关信息
//获取指定驱动器的ID号 : Get_hard_disk_id("C")
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern int GetVolumeInformation(
string lpRootPathName,
string lpVolumeNameBuffer,
int nVolumeNameSize,
ref int lpVolumeSerialNumber,
int lpMaximumComponentLength,
int lpFileSystemFlags,
string lpFileSystemNameBuffer,
int nFileSystemNameSize
);
public static string Get_hard_disk_id(string drvID)
{
const int MAX_FILENAME_LEN = 256;
int retVal = 0;
int a = 0;
int b = 0;
string str1 = null;
string str2 = null;
int i = GetVolumeInformation(
drvID + @":\",
str1,
MAX_FILENAME_LEN,
ref retVal,
a,
b,
str2,
MAX_FILENAME_LEN
);
return retVal.ToString("x");
}
//获取硬盘序列号
public static string Get_hard_disk_info()
{
string hard_disk = "";//硬盘序列号
try
{
ManagementClass mc = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
hard_disk += mo.Properties["Model"].Value.ToString() + ",";
}
}
catch { }
return hard_disk;
}
#endregion
//获取CPU序列号,如果有多个CPU,默认只返回一个
public static string Get_cpu_info()
{
string cpuInfo = "";//CPU序列号
try
{
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
}
}
catch { }
return cpuInfo;
}
//获取网卡硬件地址
public static string Get_network_card_info()
{
string network_card = ""; //网卡地址
try
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
network_card = mo["MacAddress"].ToString();
mo.Dispose();
}
}
catch { }
return network_card;
}
//获取IP地址
public static string Get_ip_address()
{
string ip = ""; //IP地址
try
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
System.Array ip_arr;
ip_arr = (System.Array)(mo.Properties["IpAddress"].Value);
ip = ip_arr.GetValue(0).ToString();
break;
}
}
}
catch { }
return ip;
}
//获取操作系统的登录用户名
public static string Get_user_name()
{
string user_name = ""; //登录用户名
try
{
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
user_name = mo["UserName"].ToString();
}
}
catch { }
return user_name;
}
//获取PC类型
public static string Get_system_type()
{
string system_type = ""; //PC类型
try
{
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
system_type = mo["SystemType"].ToString();
}
}
catch { }
return system_type;
}
//获取物理内存
public static string Get_memory_info()
{
string memory = ""; //物理内存
try
{
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
memory = mo["TotalPhysicalMemory"].ToString();
}
}
catch { }
return memory;
}
//获取计算机名称
public static string Get_computer_name()
{
string computer_name = ""; //计算机名称
try
{
computer_name = System.Environment.GetEnvironmentVariable("ComputerName");
}
catch { }
return computer_name;
}
}
}
文章来源:桂林唯创网络
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!