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

标签 c# c wmi system wmic

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

wmic csproduct get uuid

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

最佳答案

您可以从 Win32_ComputerSystemProduct 中获取通用唯一标识符 (UUID) 值WMI 类,试试这个示例。

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

关于c# - 如何从 C# 程序中检索 WMI 数据(例如 UUID)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29750894/

相关文章:

c# - 通过 WMI 更新

python - 使用 IronPython 中的 ManagementClass.Getinstances()

c# - 以特定格式将对象序列化为 XML

c# - 将类实例转换为字典以进行迭代

c# - 检查对象是否附加了 PropertyChanged 事件

c - 将 calloc 与数组一起使用并返回指针时出现问题

c - 使用 fork 和 exec 启动进程,同时将 stdout 重定向到/dev/null

c - 如何将 scanf 用于 char 数组输入?

C# 获取自定义控件内控件的事件

c# - 使用windows服务和c#检测USB驱动器插入和移除