C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C#获取计算机信息

C#实现获取计算机信息的示例代码

作者:lingxiao16888

这篇文章主要为大家详细介绍了C#实现获取计算机软硬件信息的相关知识,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考下

一、本机信息

1、本机名

 public static string getLocalhostName(){
    return Environment.GetEnvironmentVariable("COMPUTERNAME");
 }

2、获得本机MAC地址

public static string getLocalhostMac() {
    string mac = null;
    ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection queryCollection = query.Get();
    foreach (ManagementObject mo in queryCollection) {
        if (mo["IPEnabled"].ToString() == "True")
        mac = mo["MacAddress"].ToString();
    }
    return (mac);
}

3、获得计算机名

public static string getComputerName() {
    return Environment.MachineName;
}

4、显示器分辨率

public static string getResolution() {
    return SystemInformation.PrimaryMonitorMaximizedWindowSize.Width + " x " + SystemInformation.PrimaryMonitorMaximizedWindowSize.Height;
}

5、主显示器分辨率

public static string getMainResolution() {
    return SystemInformation.PrimaryMonitorSize.Width + " x " + SystemInformation.PrimaryMonitorSize.Height;
}

6、系统路径

[DllImport("kernel32")]
public static extern void GetSystemDirectory(StringBuilder SysDir, int count);
 
public static string getSystemPath() {
    const int nChars = 128;
    StringBuilder Buff = new StringBuilder(nChars);
    GetSystemDirectory(Buff, nChars);
    return Buff.ToString();
}

二、操作系统信息

1、操作系统类型

public static string getSystemType() {
    string st = "";
    ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
    ManagementObjectCollection moc = mc.GetInstances();
    foreach (ManagementObject mo in moc) {
        st = mo["SystemType"].ToString();
    }
    return st;
}

2、获得操作系统位数

public static string getOSBit() {
    if (Environment.Is64BitOperatingSystem) {
        return "64bit."
    } else {
        return  "32bit.";
    }
}

3、获得操作系统版本

public static string getOSVersion()  {
    return Environment.OSVersion.VersionString;
}

三、处理器信息

1 、处理器个数

public static string getProcessorCount()  {
    return Environment.ProcessorCount.ToString();
}

四、CPU信息

1、CPU的个数

public static int getCpuCount() {
    try {
        using (ManagementClass mCpu = new ManagementClass("Win32_Processor")) {
            ManagementObjectCollection cpus = mCpu.GetInstances();
            return cpus.Count;
        }
    } catch { }
    return -1;
}

2、CPU版本信息

public static string getCpuVersion() {
    string st = "";
    ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_Processor");
    foreach (ManagementObject mo in mos.Get()) {
        st = mo["Version"].ToString();
    }
    return st;
}

3、CPU名称信息

public static string getCpuName() {
    string st = "";
    ManagementObjectSearcher driveID = new ManagementObjectSearcher("Select * from Win32_Processor");
    foreach (ManagementObject mo in driveID.Get()) {
        st = mo["Name"].ToString();
    }
    return st;
}

4、CPU序列号

public static string getCpuSerialNumber() {
    string strCpu = null;
    ManagementClass myCpu = new ManagementClass("win32_Processor");
    ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
    foreach (ManagementObject myObject in myCpuConnection) {
        strCpu = myObject.Properties["Processorid"].Value.ToString();
        break;
    }
    return strCpu;
}

5、CPU制造厂商

public static string getCPUManufacturer() {
    string st = "";
    ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_Processor");
    foreach (ManagementObject mo in mos.Get()) {
        st = mo["Manufacturer"].ToString();
    }
    return st;
}

6、CPU频率

这里之所以使用string类型的数组,主要是因为cpu的多核

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;
}

五、显卡信息

1、显卡PNPDeviceID

public static string getVideoPNPID() {
    string st = "";
    ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_VideoController");
    foreach (ManagementObject mo in mos.Get()) {
        st = mo["PNPDeviceID"].ToString();
    }
    return st;
}

六、硬盘信息

1、设备硬盘的卷标号

public static string getDiskVolumeSerialNumber() {
    ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
    disk.Get();
    return disk.GetPropertyValue("VolumeSerialNumber").ToString();
}

2、硬盘的大小

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";
}

3、硬盘序列号

public static string getHardDiskSerialNumber() {
    try {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
        string sHardDiskSerialNumber = "";
        foreach (ManagementObject mo in searcher.Get()) {
            sHardDiskSerialNumber = mo["SerialNumber"].ToString().Trim();
            break;
        }
        return sHardDiskSerialNumber;
    }
    catch {
        return "";
    }
}

七、主板信息

1、主板序列号

public static string getBIOSSerialNumber() {
    try {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_BIOS");
        string sBIOSSerialNumber = "";
        foreach (ManagementObject mo in searcher.Get()) {
            sBIOSSerialNumber = mo["SerialNumber"].ToString().Trim();
        }
        return sBIOSSerialNumber;
    }
    catch {
        return "";
    }
}

2、主板制造厂商

public static string getBoardManufacturer() {
    SelectQuery query = new SelectQuery("Select * from Win32_BaseBoard");
    ManagementObjectSearcher mos = new ManagementObjectSearcher(query);
    ManagementObjectCollection.ManagementObjectEnumerator data = mos.Get().GetEnumerator();
    data.MoveNext();
    ManagementBaseObject board = data.Current;
    return board.GetPropertyValue("Manufacturer").ToString();
}

3、主板编号

public static string getBoardID() {
    string st = "";
    ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");
    foreach (ManagementObject mo in mos.Get()) {
        st = mo["SerialNumber"].ToString();
    }
    return st;
}

4、主板型号

public static string getBoardType() {
    string st = "";
    ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");
    foreach (ManagementObject mo in mos.Get()) {
        st = mo["Product"].ToString();
    }
    return st;
}

八、内存信息

1、物理内存大小

public static string getPhysicalMemory() {
    string st = "";
    ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
    ManagementObjectCollection moc = mc.GetInstances();
    foreach (ManagementObject mo in moc) {
        st = mo["TotalPhysicalMemory"].ToString();
    }
    return st;
}

2、内存利用率

[DllImport("kernel32")]
private static extern void GlobalMemoryStatus(ref StorageInfo memibfo);
 
[StructLayout(LayoutKind.Sequential)]
public struct StorageInfo //此处全是以字节为单位 {
    public uint dwMemoryLoad;//内存使用率
}
 
public static string getUtilizationRate() {
    StorageInfo memInfor = new StorageInfo();
    GlobalMemoryStatus(ref memInfor);
    return memInfor.dwMemoryLoad.ToString("0.0");
}

九、网卡信息

1、网卡硬件地址

public static string getMacAddress() {
    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;
}

2、网卡地址

public static string getNetCardMACAddress() {
    try {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter WHERE ((MACAddress Is Not NULL) AND (Manufacturer <> 'Microsoft'))");
        string NetCardMACAddress = "";
        foreach (ManagementObject mo in searcher.Get()) {
            NetCardMACAddress = mo["MACAddress"].ToString().Trim();
        }
        return NetCardMACAddress;
    }
    catch {
        return "";
    }
}

3、适配器信息

public static NetworkInterface[] getNetWorkInfo() {
    NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
    return adapters;
}
// 设置内容向文本框
public void setContent() {
    tb_net_work_adapter_num.Text = adapters.Length.ToString();
    int index = 0;
    foreach (NetworkInterface adapter in adapters) {
        index++;
        //显示网络适配器描述信息、名称、类型、速度、MAC 地址
        tb_net_wark_adapter_info.Text = tb_net_wark_adapter_info.Text + ("---------------------第" + index + "个适配器信息---------------------") + "\n";
        tb_net_wark_adapter_info.Text = tb_net_wark_adapter_info.Text + ("描述信息:" + adapter.Name) + "\n";
        tb_net_wark_adapter_info.Text = tb_net_wark_adapter_info.Text + ("类型:" + adapter.NetworkInterfaceType) + "\n";
        tb_net_wark_adapter_info.Text = tb_net_wark_adapter_info.Text + ("速度:" + adapter.Speed / 1000 / 1000 + "MB") + "\n";
        tb_net_wark_adapter_info.Text = tb_net_wark_adapter_info.Text + ("MAC 地址:" + adapter.GetPhysicalAddress()) + "\n";
        //获取IPInterfaceProperties实例
        IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
        //获取并显示DNS服务器IP地址信息
        IPAddressCollection dnsServers = adapterProperties.DnsAddresses;
        if (dnsServers.Count > 0) {
            foreach (IPAddress dns in dnsServers) {
                tb_net_wark_adapter_info.Text = tb_net_wark_adapter_info.Text + ("DNS 服务器IP地址:" + dns + "\n");
            }
        } else {
            tb_net_wark_adapter_info.Text = tb_net_wark_adapter_info.Text + ("DNS 服务器IP地址:" + "\n");
        }
    }
}

结果样例

---------------------第1个适配器信息---------------------
描述信息:以太网
类型:Ethernet
速度:0MB
MAC 地址:9883892C3E84
DNS 服务器IP地址:192.168.0.0
---------------------第2个适配器信息---------------------
描述信息:VirtualBox Host-Only Network #3
类型:Ethernet
速度:1000MB
MAC 地址:0A002700000B
DNS 服务器IP地址:fec0:0:0:ffff::1%1
DNS 服务器IP地址:fec0:0:0:ffff::2%1
DNS 服务器IP地址:fec0:0:0:ffff::3%1

十、声卡信息

1、声卡PNPDeviceID

public static string getSoundPNPID() {
    string st = "";
    ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_SoundDevice");
    foreach (ManagementObject mo in mos.Get()) {
        st = mo["PNPDeviceID"].ToString();
    }
    return st;
}

十一、其他信息

1、鼠标按钮个数

public static string getMouseButtons()  {
    return SystemInformation.MouseButtons.ToString();
}

十二、网络信息

1、网络连接

public static string getIsHasNet() {
    if (SystemInformation.Network) {
        return  "网络连接:已连接";
    } else {
        return "网络连接:未连接";
    }
}

2、IP地址

public static string getIP() {
    try {
		string st = "";
		ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
		ManagementObjectCollection moc = mc.GetInstances();
		foreach (ManagementObject mo in moc) {
			if ((bool)mo["IPEnabled"] == true) {
				System.Array ar;
				ar = (System.Array)(mo.Properties["IpAddress"].Value);
				st = ar.GetValue(0).ToString();
				break;
			}
		}
		moc = null;
		mc = null;
		return st;
    } catch {
		return "unknow";
    }
}

以上就是C#实现获取计算机信息的示例代码的详细内容,更多关于C#获取计算机信息的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文