如何从C#程序中检索WMI数据,如UUID?

时间:2015-04-20 14:38:00

标签: c# c wmi system wmic

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

wmic csproduct get uuid

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

1 个答案:

答案 0 :(得分:3)

您可以从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();
        }
    }
}