詳解如何使用C#獲取計(jì)算機(jī)信息
更新時(shí)間:2024年10月21日 11:53:42 作者:天天代碼碼天天
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)獲取計(jì)算機(jī)信息,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下
效果

項(xiàng)目

代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
namespace 獲取計(jì)算機(jī)信息
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 獲取計(jì)算機(jī)硬件信息
/// </summary>
/// <param name="path">部件</param>
/// <param name="key">鍵值</param>
/// <returns></returns>
public string GetComputerHardWareInfo(string path, string key)
{
try
{
ManagementClass managementClass = new ManagementClass(path);
ManagementObjectCollection moc = managementClass.GetInstances();
PropertyDataCollection properties = managementClass.Properties;
foreach (var property in properties)
{
if (property.Name == key)
{
foreach (var mo in moc)
{
return mo.Properties[property.Name].Value.ToString();
}
}
}
}
catch (Exception ex)
{
//記錄異常信息
}
return string.Empty;
}
private void button1_Click(object sender, EventArgs e)
{
//獲取CPU ID
string CPU_ID = GetComputerHardWareInfo("Win32_Processor", "ProcessorId");
//獲取主板序列號(hào)
string Board_SN = GetComputerHardWareInfo("Win32_BaseBoard", "SerialNumber");
//獲取硬盤(pán)序列號(hào)
string Disk_SN = GetComputerHardWareInfo("Win32_DiskDrive", "Model");
string UUID = GetComputerHardWareInfo("Win32_ComputerSystemProduct", "UUID");
// 獲取機(jī)器名
string MachineName = Environment.MachineName;
string OSVersion = Environment.OSVersion.VersionString.ToString();
string UserName = Environment.UserName;
string OperatingSystem = GetComputerHardWareInfo("Win32_OperatingSystem", "Caption");
StringBuilder sb = new StringBuilder();
sb.AppendLine(string.Format("CPU_ID:{0}", CPU_ID));
sb.AppendLine(string.Format("Board_SN:{0}", Board_SN));
sb.AppendLine(string.Format("Disk_SN:{0}", Disk_SN));
sb.AppendLine(string.Format("UUID:{0}", UUID));
sb.AppendLine(string.Format("MachineName:{0}", MachineName));
sb.AppendLine(string.Format("OSVersion:{0}", OSVersion));
sb.AppendLine(string.Format("UserName:{0}", UserName));
sb.AppendLine(string.Format("OperatingSystem:{0}", OperatingSystem));
textBox1.Text = sb.ToString();
}
}
}到此這篇關(guān)于詳解如何使用C#獲取計(jì)算機(jī)信息的文章就介紹到這了,更多相關(guān)C#獲取計(jì)算機(jī)信息內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用SignalR實(shí)現(xiàn)與前端vue實(shí)時(shí)通信的示例代碼
SignalR 是 ASP.NET Core 的一個(gè)庫(kù),它簡(jiǎn)化了在應(yīng)用程序中添加實(shí)時(shí)通信的過(guò)程,無(wú)論是聊天應(yīng)用、實(shí)時(shí)游戲還是協(xié)作工具,SignalR 都能提供高效且易于實(shí)現(xiàn)的解決方案,本文給大家介紹了C#使用SignalR實(shí)現(xiàn)與前端vue實(shí)時(shí)通信的實(shí)現(xiàn),需要的朋友可以參考下2024-10-10
C#利用GDI+給圖片添加文字(文字自適應(yīng)矩形區(qū)域)
這篇文章主要給大家介紹了關(guān)于C#利用GDI+給圖片添加文字(文字自適應(yīng)矩形區(qū)域)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2018-04-04
C#實(shí)現(xiàn)人民幣大寫(xiě)轉(zhuǎn)換示例代碼
這篇文章主要介紹了C#實(shí)現(xiàn)人民幣大寫(xiě)轉(zhuǎn)換,需要的朋友可以參考使用2013-12-12
DevExpress實(shí)現(xiàn)TreeList父子節(jié)點(diǎn)CheckState狀態(tài)同步的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)TreeList父子節(jié)點(diǎn)CheckState狀態(tài)同步的方法,需要的朋友可以參考下2014-08-08

