search
HomeBackend DevelopmentC#.Net TutorialC# programming to obtain client computer hardware and system information function code case analysis

This article mainly introduces the function of C#Programming to obtain the client computer hardware and system information, which can realize the client system CPU, hard disk, motherboard and other hardware information and client Friends in need can refer to the operation skills of operating system, IP, MAC and other information.

This article describes the function of C# programming to obtain client computer hardware and system information. Share it with everyone for your reference, the details are as follows:

C# is used here to obtain the client computer hardware and system information, including CPU, hard disk, IP, MAC address, operating system, etc.

1. The project references the System.Management library.

2. Create the HardwareHandler.cs class file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
namespace MyStudy.Utility
{
  /// <summary>
  /// 计算机硬件处理类
  /// </summary>
  public class HardwareHandler
  {
    public enum WMIPath
    {
      // 硬件
      Win32_Processor,     // CPU 处理器
      Win32_PhysicalMemory,  // 物理内存条
      Win32_Keyboard,     // 键盘
      Win32_PointingDevice,  // 点输入设备,包括鼠标。
      Win32_FloppyDrive,    // 软盘驱动器
      Win32_DiskDrive,     // 硬盘驱动器
      Win32_CDROMDrive,    // 光盘驱动器
      Win32_BaseBoard,     // 主板
      Win32_BIOS,       // BIOS 芯片
      Win32_ParallelPort,   // 并口
      Win32_SerialPort,    // 串口
      Win32_SerialPortConfiguration, // 串口配置
      Win32_SoundDevice,    // 多媒体设置,一般指声卡。
      Win32_SystemSlot,    // 主板插槽 (ISA & PCI & AGP)
      Win32_USBController,   // USB 控制器
      Win32_NetworkAdapter,  // 网络适配器
      Win32_NetworkAdapterConfiguration, // 网络适配器设置
      Win32_Printer,      // 打印机
      Win32_PrinterConfiguration, // 打印机设置
      Win32_PrintJob,     // 打印机任务
      Win32_TCPIPPrinterPort, // 打印机端口
      Win32_POTSModem,     // MODEM
      Win32_POTSModemToSerialPort, // MODEM 端口
      Win32_DesktopMonitor,  // 显示器
      Win32_DisplayConfiguration, // 显卡
      Win32_DisplayControllerConfiguration, // 显卡设置
      Win32_VideoController, // 显卡细节。
      Win32_VideoSettings,  // 显卡支持的显示模式。
      // 操作系统
      Win32_TimeZone,     // 时区
      Win32_SystemDriver,   // 驱动程序
      Win32_DiskPartition,  // 磁盘分区
      Win32_LogicalDisk,   // 逻辑磁盘
      Win32_LogicalDiskToPartition,   // 逻辑磁盘所在分区及始末位置。
      Win32_LogicalMemoryConfiguration, // 逻辑内存配置
      Win32_PageFile,     // 系统页文件信息
      Win32_PageFileSetting, // 页文件设置
      Win32_BootConfiguration, // 系统启动配置
      Win32_ComputerSystem,  // 计算机信息简要
      Win32_OperatingSystem, // 操作系统信息
      Win32_StartupCommand,  // 系统自动启动程序
      Win32_Service,     // 系统安装的服务
      Win32_Group,      // 系统管理组
      Win32_GroupUser,    // 系统组帐号
      Win32_UserAccount,   // 用户帐号
      Win32_Process,     // 系统进程
      Win32_Thread,      // 系统线程
      Win32_Share,      // 共享
      Win32_NetworkClient,  // 已安装的网络客户端
      Win32_NetworkProtocol, // 已安装的网络协议
    }
    /// <summary>
    /// Cpu信息
    /// </summary>
    /// <returns></returns>
    public void CpuInfo()
    {
      try
      {
        ManagementClass mc = new ManagementClass(WMIPath.Win32_Processor.ToString());
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
          Console.WriteLine("CPU编号:" + mo.Properties["ProcessorId"].Value);
          Console.WriteLine("CPU型号:" + mo.Properties["Name"].Value);
          Console.WriteLine("CPU状态:" + mo.Properties["Status"].Value);
          Console.WriteLine("主机名称:" + mo.Properties["SystemName"].Value);
        }
      }
      catch
      {
        Console.WriteLine("Erroe");
      }
    }
    /// <summary>
    /// 主板信息
    /// </summary>
    public void MainBoardInfo()
    {
      try
      {
        ManagementClass mc = new ManagementClass(WMIPath.Win32_BaseBoard.ToString());
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
          Console.WriteLine("主板ID:" + mo.Properties["SerialNumber"].Value);
          Console.WriteLine("制造商:" + mo.Properties["Manufacturer"].Value);
          Console.WriteLine("型号:" + mo.Properties["Product"].Value);
          Console.WriteLine("版本:" + mo.Properties["Version"].Value);
        }
      }
      catch
      {
        Console.WriteLine("Erroe");
      }
    }
    /// <summary>
    /// 硬盘信息
    /// </summary>
    public void DiskDriveInfo()
    {
      try
      {
        ManagementClass mc = new ManagementClass(WMIPath.Win32_DiskDrive.ToString());
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
          Console.WriteLine("硬盘SN:" + mo.Properties["SerialNumber"].Value);
          Console.WriteLine("型号:" + mo.Properties["Model"].Value);
          Console.WriteLine("大小:" + Convert.ToDouble(mo.Properties["Size"].Value) / (1024 * 1024 * 1024));
        }
      }
      catch
      {
        Console.WriteLine("Erroe");
      }
    }
    /// <summary>
    /// 网络连接信息
    /// </summary>
    public void NetworkInfo()
    {
      try
      {
        ManagementClass mc = new ManagementClass(WMIPath.Win32_NetworkAdapterConfiguration.ToString());
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
          Console.WriteLine("MAC地址:" + mo.Properties["MACAddress"].Value);
          Console.WriteLine("IP地址:" + mo.Properties["IPAddress"].Value);
        }
      }
      catch
      {
        Console.WriteLine("Erroe");
      }
    }
    /// <summary>
    /// 操作系统信息
    /// </summary>
    public void OsInfo()
    {
      try
      {
        ManagementClass mc = new ManagementClass(WMIPath.Win32_OperatingSystem.ToString());
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
          Console.WriteLine("操作系统:" + mo.Properties["Name"].Value);
          Console.WriteLine("版本:" + mo.Properties["Version"].Value);
          Console.WriteLine("系统目录:" + mo.Properties["SystemDirectory"].Value);
        }
      }
      catch
      {
        Console.WriteLine("Erroe");
      }
    }
  }
}

The above is the detailed content of C# programming to obtain client computer hardware and system information function code case analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
C# .NET Development: A Beginner's Guide to Getting StartedC# .NET Development: A Beginner's Guide to Getting StartedApr 18, 2025 am 12:17 AM

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

C# and .NET: Understanding the Relationship Between the TwoC# and .NET: Understanding the Relationship Between the TwoApr 17, 2025 am 12:07 AM

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

The Continued Relevance of C# .NET: A Look at Current UsageThe Continued Relevance of C# .NET: A Look at Current UsageApr 16, 2025 am 12:07 AM

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

From Web to Desktop: The Versatility of C# .NETFrom Web to Desktop: The Versatility of C# .NETApr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# .NET and the Future: Adapting to New TechnologiesC# .NET and the Future: Adapting to New TechnologiesApr 14, 2025 am 12:06 AM

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

Is C# .NET Right for You? Evaluating its ApplicabilityIs C# .NET Right for You? Evaluating its ApplicabilityApr 13, 2025 am 12:03 AM

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

C# Code within .NET: Exploring the Programming ProcessC# Code within .NET: Exploring the Programming ProcessApr 12, 2025 am 12:02 AM

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

C# .NET: Exploring Core Concepts and Programming FundamentalsC# .NET: Exploring Core Concepts and Programming FundamentalsApr 10, 2025 am 09:32 AM

C# is a modern, object-oriented programming language developed by Microsoft and as part of the .NET framework. 1.C# supports object-oriented programming (OOP), including encapsulation, inheritance and polymorphism. 2. Asynchronous programming in C# is implemented through async and await keywords to improve application responsiveness. 3. Use LINQ to process data collections concisely. 4. Common errors include null reference exceptions and index out-of-range exceptions. Debugging skills include using a debugger and exception handling. 5. Performance optimization includes using StringBuilder and avoiding unnecessary packing and unboxing.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.