How to retrieve WMI data such as UUID from a C# program?

强颜欢笑 提交于 2019-12-12 02:12:57

问题


To retrieve system's UUID we have the option of WMIC command-line utility

wmic csproduct get uuid

How to retrieve the same uuid from a system using C or C# program or using .dll?


回答1:


You can get the Universally unique identifier (UUID) value from the Win32_ComputerSystemProduct WMI class, try this sample.

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();
        }
    }
}


来源:https://stackoverflow.com/questions/29750894/how-to-retrieve-wmi-data-such-as-uuid-from-a-c-sharp-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!