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频率

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#获取计算机信息的资料陆续记录学习!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值