如何从 C# 程序中检索 WMI 数据(如 UUID)



要检索系统的UUID,我们可以选择WMIC命令行实用程序

wmic csproduct get uuid

如何使用 C 或 C# 程序或使用.dll从系统中检索相同的 uuid?

可以从

Win32_ComputerSystemProduct WMI 类中获取通用唯一标识符 (UUID) 值,请尝试此示例。

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;
namespace GetWMI_Info
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string ComputerName = "localhost";
                ManagementScope Scope;                
                Scope = new ManagementScope(String.Format("\\{0}\root\CIMV2", ComputerName), null);
                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT UUID FROM Win32_ComputerSystemProduct");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);
                foreach (ManagementObject WmiObject in Searcher.Get())
                {
                    Console.WriteLine("{0,-35} {1,-40}","UUID",WmiObject["UUID"]);// String                     
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }
}

最新更新