Csharp取得系統資訊資料

在開發程式時,有時需要知道部份的硬體資訊等,像是ProcessId的編號、System Name等,可以利用System Manager的查詢來取得其內容。底下是可查詢到的資料

CsharpCpuInfo

AddressWidth:64
Architecture:9
Availability:3
Caption:Intel64 Family 6 Model 58 Stepping 9
CpuStatus:1
CreationClassName:Win32_Processor
CurrentClockSpeed:3601
CurrentVoltage:11
DataWidth:64
Description:Intel64 Family 6 Model 58 Stepping 9
DeviceID:CPU0
ExtClock:100
Family:198
L2CacheSize:1024
L3CacheSize:6144
L3CacheSpeed:0
Level:6
LoadPercentage:4
Manufacturer:GenuineIntel
MaxClockSpeed:3601
Name:Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz
NumberOfCores:4
NumberOfLogicalProcessors:4
PowerManagementSupported:False
Processor序號:BFEBFBCF001306A9
ProcessorType:3
Revision:14857
Role:CPU
SocketDesignation:Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz
Status:OK
StatusInfo:3
SystemCreationClassName:Win32_ComputerSystem
SystemName:MyPC
UpgradeMethod:1
Version:

ProcessId的序號並非唯一而且一定取的到的,如果需要唯一值,建議可以使用Mac Address會比較好。

在使用時System.Manager需要被參考

CsharpImportSystemManager

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            
            try
            {

               

                ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Processor");



                foreach (ManagementObject managementObject in searcher.Get())
                {

                    foreach (PropertyData property in managementObject.Properties)
                    {

                        if (property.Value == null)

                            continue;

                        if (property.Name.ToLower() == "ProcessorId".ToLower())   
                            Console.WriteLine("Processor序號:"+property.Value.ToString());
                        else
                            Console.WriteLine(property.Name+":"+property.Value.ToString());

                    }

                }
               

            }

            catch (Exception ex)
            {

                Console.Write(ex);

            }
            Console.Read();
        }
    }
}

取得Mac Address

            string mac= "";
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            //for each j you can get the MAC ,nics[0]第一張網卡,1則為第二張網卡
            PhysicalAddress address = nics[0].GetPhysicalAddress();
            byte[] bytes = address.GetAddressBytes();
            for (int i = 0; i < bytes.Length; i++)
            {
                // Display the physical address in hexadecimal.
                mac+=bytes[i].ToString("X2");
                // Insert a hyphen after each byte, unless we are at the end of the
                // address.
                if (i != bytes.Length - 1)
                {
                    mac+= ("-");
                }
            }
            Console.WriteLine(mac);

發表迴響