以编程方式查找机器上的核心数

发布于 2024-07-06 00:57:15 字数 84 浏览 11 评论 0原文

有没有办法以与平台无关的方式从 C/C++ 确定机器有多少个核心? 如果不存在这样的东西,那么如何根据平台(Windows/*nix/Mac)确定它呢?

Is there a way to determine how many cores a machine has from C/C++ in a platform-independent way? If no such thing exists, what about determining it per-platform (Windows/*nix/Mac)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(21

∞觅青森が 2024-07-13 00:57:16

C++11

#include <thread>

//may return 0 when not able to detect
const auto processor_count = std::thread::hardware_concurrency();

参考:std::thread::hardware_concurrency


在 C++ 中在 C++11 之前,没有可移植的方法。 相反,您需要使用以下一种或多种方法(由适当的 #ifdef 行保护):

  • Win32

    SYSTEM_INFO 系统信息; 
      GetSystemInfo(&sysinfo); 
      int numCPU = sysinfo.dwNumberOfProcessors; 
      
  • Linux、Solaris、AIX 和 Mac OS X >=10.4(即 Tiger 及以上版本)

    int numCPU = sysconf(_SC_NPROCESSORS_ONLN); 
      
  • FreeBSD、MacOS X、NetBSD、OpenBSD 等

    int mib[4]; 
      int numCPU; 
      std::size_t len = sizeof(numCPU);  
    
      /* 设置 hw.ncpu 的 mib */ 
      mib[0] = CTL_HW; 
      mib[1] = HW_AVAILCPU;   // 或者,尝试 HW_NCPU; 
    
      /* 获取系统CPU数量 */ 
      sysctl(mib, 2, &numCPU, &len, NULL, 0); 
    
      if (numCPU < 1)  
      { 
          mib[1] = HW_NCPU; 
          sysctl(mib, 2, &numCPU, &len, NULL, 0); 
          if (numCPU < 1) 
              CPU 数量 = 1; 
      } 
      
  • HPUX

    int numCPU = mctl(MPC_GETNUMSPUS, NULL, NULL); 
      
  • IRIX

    int numCPU = sysconf(_SC_NPROC_ONLN); 
      
  • Objective-C(Mac OS X >=10.5 或 iOS)

    NSUInteger a = [[NSProcessInfo processInfo]processorCount]; 
      NSUInteger b = [[NSProcessInfo processInfo] activeProcessorCount]; 
      

C++11

#include <thread>

//may return 0 when not able to detect
const auto processor_count = std::thread::hardware_concurrency();

Reference: std::thread::hardware_concurrency


In C++ prior to C++11, there's no portable way. Instead, you'll need to use one or more of the following methods (guarded by appropriate #ifdef lines):

  • Win32

    SYSTEM_INFO sysinfo;
    GetSystemInfo(&sysinfo);
    int numCPU = sysinfo.dwNumberOfProcessors;
    
  • Linux, Solaris, AIX and Mac OS X >=10.4 (i.e. Tiger onwards)

    int numCPU = sysconf(_SC_NPROCESSORS_ONLN);
    
  • FreeBSD, MacOS X, NetBSD, OpenBSD, etc.

    int mib[4];
    int numCPU;
    std::size_t len = sizeof(numCPU); 
    
    /* set the mib for hw.ncpu */
    mib[0] = CTL_HW;
    mib[1] = HW_AVAILCPU;  // alternatively, try HW_NCPU;
    
    /* get the number of CPUs from the system */
    sysctl(mib, 2, &numCPU, &len, NULL, 0);
    
    if (numCPU < 1) 
    {
        mib[1] = HW_NCPU;
        sysctl(mib, 2, &numCPU, &len, NULL, 0);
        if (numCPU < 1)
            numCPU = 1;
    }
    
  • HPUX

    int numCPU = mpctl(MPC_GETNUMSPUS, NULL, NULL);
    
  • IRIX

    int numCPU = sysconf(_SC_NPROC_ONLN);
    
  • Objective-C (Mac OS X >=10.5 or iOS)

    NSUInteger a = [[NSProcessInfo processInfo] processorCount];
    NSUInteger b = [[NSProcessInfo processInfo] activeProcessorCount];
    
卷耳 2024-07-13 00:57:16

(几乎)C 代码中的平台独立函数

#ifdef _WIN32
#include <windows.h>
#elif MACOS
#include <sys/param.h>
#include <sys/sysctl.h>
#else
#include <unistd.h>
#endif

int getNumCores() {
#ifdef WIN32
    SYSTEM_INFO sysinfo;
    GetSystemInfo(&sysinfo);
    return sysinfo.dwNumberOfProcessors;
#elif MACOS
    int nm[2];
    size_t len = 4;
    uint32_t count;

    nm[0] = CTL_HW; nm[1] = HW_AVAILCPU;
    sysctl(nm, 2, &count, &len, NULL, 0);

    if(count < 1) {
        nm[1] = HW_NCPU;
        sysctl(nm, 2, &count, &len, NULL, 0);
        if(count < 1) { count = 1; }
    }
    return count;
#else
    return sysconf(_SC_NPROCESSORS_ONLN);
#endif
}

(Almost) Platform Independent function in c-code

#ifdef _WIN32
#include <windows.h>
#elif MACOS
#include <sys/param.h>
#include <sys/sysctl.h>
#else
#include <unistd.h>
#endif

int getNumCores() {
#ifdef WIN32
    SYSTEM_INFO sysinfo;
    GetSystemInfo(&sysinfo);
    return sysinfo.dwNumberOfProcessors;
#elif MACOS
    int nm[2];
    size_t len = 4;
    uint32_t count;

    nm[0] = CTL_HW; nm[1] = HW_AVAILCPU;
    sysctl(nm, 2, &count, &len, NULL, 0);

    if(count < 1) {
        nm[1] = HW_NCPU;
        sysctl(nm, 2, &count, &len, NULL, 0);
        if(count < 1) { count = 1; }
    }
    return count;
#else
    return sysconf(_SC_NPROCESSORS_ONLN);
#endif
}
假情假意假温柔 2024-07-13 00:57:16

此功能是 C++11 标准的一部分。

#include <thread>

unsigned int nthreads = std::thread::hardware_concurrency();

对于较旧的编译器,您可以使用 Boost.Thread图书馆。

#include <boost/thread.hpp>

unsigned int nthreads = boost::thread::hardware_concurrency();

无论哪种情况,hardware_concurrency() 都会根据 CPU 核心和超线程单元的数量返回硬件能够同时执行的线程数。

This functionality is part of the C++11 standard.

#include <thread>

unsigned int nthreads = std::thread::hardware_concurrency();

For older compilers, you can use the Boost.Thread library.

#include <boost/thread.hpp>

unsigned int nthreads = boost::thread::hardware_concurrency();

In either case, hardware_concurrency() returns the number of threads that the hardware is capable of executing concurrently based on the number of CPU cores and hyper-threading units.

情未る 2024-07-13 00:57:16

许多平台(包括 Visual Studio 2005)都支持 OpenMP,并且它提供了一个

int omp_get_num_procs();

返回可用处理器/内核数量的函数在通话时。

OpenMP is supported on many platforms (including Visual Studio 2005) and it offers a

int omp_get_num_procs();

function that returns the number of processors/cores available at the time of call.

深巷少女 2024-07-13 00:57:16

如果您具有汇编语言访问权限,则可以使用 CPUID 指令来获取有关 CPU 的各种信息。 它可以在操作系统之间移植,但您需要使用制造商特定的信息来确定如何查找内核数量。 这是一个文档,描述了如何在 Intel 芯片上执行此操作的第 11 页描述了AMD 规范。

If you have assembly-language access, you can use the CPUID instruction to get all sorts of information about the CPU. It's portable between operating systems, though you'll need to use manufacturer-specific information to determine how to find the number of cores. Here's a document that describes how to do it on Intel chips, and page 11 of this one describes the AMD specification.

ゝ偶尔ゞ 2024-07-13 00:57:16

在 Linux 上,您可以读取 /proc/cpuinfo 文件并计算内核数。

On Linux, you can read the /proc/cpuinfo file and count the cores.

迷荒 2024-07-13 00:57:16

Windows(x64 和 Win32)和 C++17

共享单个处理器核心的逻辑处理器组的数量。(使用 GetLogicalProcessorInformationEx,请参阅 GetLogicalProcessorInformation 以及)

size_t NumberOfPhysicalCores() noexcept {

    DWORD length = 0;
    const BOOL result_first = GetLogicalProcessorInformationEx(RelationProcessorCore, nullptr, &length);
    assert(GetLastError() == ERROR_INSUFFICIENT_BUFFER);

    std::unique_ptr< std::byte[] > buffer(new std::byte[length]);
    const PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX info = 
            reinterpret_cast< PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX >(buffer.get());

    const BOOL result_second = GetLogicalProcessorInformationEx(RelationProcessorCore, info, &length);
    assert(result_second != FALSE);

    size_t nb_physical_cores = 0;
    size_t offset = 0;
    do {
        const PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX current_info =
            reinterpret_cast< PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX >(buffer.get() + offset);
        offset += current_info->Size;
        ++nb_physical_cores;
    } while (offset < length);
        
    return nb_physical_cores;
}

请注意 NumberOfPhysicalCores 的实现恕我直言,这远非微不足道(即“使用 GetLogicalProcessorInformationGetLogicalProcessorInformationEx”)。 相反,如果阅读 MSDN 上的文档(显式显示 GetLogicalProcessorInformation 和隐式显示 GetLogicalProcessorInformationEx),就会发现这一点相当微妙。

逻辑处理器的数量.(使用 GetSystemInfo)

size_t NumberOfSystemCores() noexcept {
    SYSTEM_INFO system_info;
    ZeroMemory(&system_info, sizeof(system_info));
    
    GetSystemInfo(&system_info);
    
    return static_cast< size_t >(system_info.dwNumberOfProcessors);
}

请注意,这两种方法都可以轻松转换为 C/C++98/C++03/C++11/C++14。

Windows (x64 and Win32) and C++17

The number of groups of logical processors sharing a single processor core. (Using GetLogicalProcessorInformationEx, see GetLogicalProcessorInformation as well)

size_t NumberOfPhysicalCores() noexcept {

    DWORD length = 0;
    const BOOL result_first = GetLogicalProcessorInformationEx(RelationProcessorCore, nullptr, &length);
    assert(GetLastError() == ERROR_INSUFFICIENT_BUFFER);

    std::unique_ptr< std::byte[] > buffer(new std::byte[length]);
    const PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX info = 
            reinterpret_cast< PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX >(buffer.get());

    const BOOL result_second = GetLogicalProcessorInformationEx(RelationProcessorCore, info, &length);
    assert(result_second != FALSE);

    size_t nb_physical_cores = 0;
    size_t offset = 0;
    do {
        const PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX current_info =
            reinterpret_cast< PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX >(buffer.get() + offset);
        offset += current_info->Size;
        ++nb_physical_cores;
    } while (offset < length);
        
    return nb_physical_cores;
}

Note that the implementation of NumberOfPhysicalCores is IMHO far from trivial (i.e. "use GetLogicalProcessorInformation or GetLogicalProcessorInformationEx"). Instead it is rather subtle if one reads the documentation (explicitly present for GetLogicalProcessorInformation and implicitly present for GetLogicalProcessorInformationEx) at MSDN.

The number of logical processors. (Using GetSystemInfo)

size_t NumberOfSystemCores() noexcept {
    SYSTEM_INFO system_info;
    ZeroMemory(&system_info, sizeof(system_info));
    
    GetSystemInfo(&system_info);
    
    return static_cast< size_t >(system_info.dwNumberOfProcessors);
}

Note that both methods can easily be converted to C/C++98/C++03/C++11/C++14.

浅黛梨妆こ 2024-07-13 00:57:16

另一种 Windows 秘诀:使用系统范围的环境变量 NUMBER_OF_PROCESSORS

printf("%d\n", atoi(getenv("NUMBER_OF_PROCESSORS")));

One more Windows recipe: use system-wide environment variable NUMBER_OF_PROCESSORS:

printf("%d\n", atoi(getenv("NUMBER_OF_PROCESSORS")));
只等公子 2024-07-13 00:57:16

请注意,“核心数”可能不是一个特别有用的数字,您可能需要对其进行更多限定。 您想如何计算多线程 CPU,例如 Intel HT、IBM Power5 和 Power6,以及最著名的 Sun 的 Niagara/UltraSparc T1 和 T2? 或者更有趣的是,MIPS 1004k 具有两级硬件线程(管理程序级和用户级)...更不用说当您进入虚拟机管理程序支持的系统时会发生什么,其中硬件可能有数十个 CPU,但您的特定操作系统只看到几个。

您所能期望的最好结果就是告诉您本地操作系统分区中拥有的逻辑处理单元的数量。 除非您是虚拟机管理程序,否则忘记看到真实的机器。 今天这条规则的唯一例外是在 x86 领域,但非虚拟机的终结即将到来......

Note that "number of cores" might not be a particularly useful number, you might have to qualify it a bit more. How do you want to count multi-threaded CPUs such as Intel HT, IBM Power5 and Power6, and most famously, Sun's Niagara/UltraSparc T1 and T2? Or even more interesting, the MIPS 1004k with its two levels of hardware threading (supervisor AND user-level)... Not to mention what happens when you move into hypervisor-supported systems where the hardware might have tens of CPUs but your particular OS only sees a few.

The best you can hope for is to tell the number of logical processing units that you have in your local OS partition. Forget about seeing the true machine unless you are a hypervisor. The only exception to this rule today is in x86 land, but the end of non-virtual machines is coming fast...

煮酒 2024-07-13 00:57:16

您可能无法以独立于平台的方式获得它。 Windows 你可以获得处理器的数量。

Win32 系统信息

You probably won't be able to get it in a platform independent way. Windows you get number of processors.

Win32 System Information

书信已泛黄 2024-07-13 00:57:16

有关 OS X 的更多信息:sysconf(_SC_NPROCESSORS_ONLN) 仅适用于 >= 10.5 的版本,不适用于 10.4。

另一种选择是 HW_AVAILCPU/sysctl() BSD 代码,该代码在版本 >= 10.2 上可用。

More on OS X: sysconf(_SC_NPROCESSORS_ONLN) is available only versions >= 10.5, not 10.4.

An alternative is the HW_AVAILCPU/sysctl() BSD code which is available on versions >= 10.2.

橘和柠 2024-07-13 00:57:16

与 C++ 无关,但在 Linux 上我通常这样做:

grep processor /proc/cpuinfo | wc -l

对于 bash/perl/python/ruby 等脚本语言很方便。

Unrelated to C++, but on Linux I usually do:

grep processor /proc/cpuinfo | wc -l

Handy for scripting languages like bash/perl/python/ruby.

南巷近海 2024-07-13 00:57:16

Windows Server 2003 及更高版本允许您利用 GetLogicalProcessorInformation 函数

http://msdn.microsoft .com/en-us/library/ms683194.aspx

Windows Server 2003 and later lets you leverage the GetLogicalProcessorInformation function

http://msdn.microsoft.com/en-us/library/ms683194.aspx

染火枫林 2024-07-13 00:57:16

在 Linux 上,使用 _SC_NPROCESSORS_ONLN 可能不安全,因为它不是 POSIX 标准和 sysconf 手册中也有说明。 因此,_SC_NPROCESSORS_ONLN 可能不存在:

 These values also exist, but may not be standard.

     [...]     

     - _SC_NPROCESSORS_CONF
              The number of processors configured.   
     - _SC_NPROCESSORS_ONLN
              The number of processors currently online (available).

一种简单的方法是读取 /proc/stat/proc/cpuinfo 并进行计数他们:

#include<unistd.h>
#include<stdio.h>

int main(void)
{
char str[256];
int procCount = -1; // to offset for the first entry
FILE *fp;

if( (fp = fopen("/proc/stat", "r")) )
{
  while(fgets(str, sizeof str, fp))
  if( !memcmp(str, "cpu", 3) ) procCount++;
}

if ( procCount == -1) 
{ 
printf("Unable to get proc count. Defaulting to 2");
procCount=2;
}

printf("Proc Count:%d\n", procCount);
return 0;
}

使用 /proc/cpuinfo

#include<unistd.h>
#include<stdio.h>

int main(void)
{
char str[256];
int procCount = 0;
FILE *fp;

if( (fp = fopen("/proc/cpuinfo", "r")) )
{
  while(fgets(str, sizeof str, fp))
  if( !memcmp(str, "processor", 9) ) procCount++;
}

if ( !procCount ) 
{ 
printf("Unable to get proc count. Defaulting to 2");
procCount=2;
}

printf("Proc Count:%d\n", procCount);
return 0;
}

在 shell 中使用 grep 的方法相同:

grep -c ^processor /proc/cpuinfo

或者

grep -c ^cpu /proc/stat # subtract 1 from the result

On Linux, it's may not be safe to to use _SC_NPROCESSORS_ONLN as it's not part of POSIX standard and the sysconf manual states as much. So there's a possibility that _SC_NPROCESSORS_ONLN may not be present:

 These values also exist, but may not be standard.

     [...]     

     - _SC_NPROCESSORS_CONF
              The number of processors configured.   
     - _SC_NPROCESSORS_ONLN
              The number of processors currently online (available).

A simple approach would be to read /proc/stat or /proc/cpuinfo and count them:

#include<unistd.h>
#include<stdio.h>

int main(void)
{
char str[256];
int procCount = -1; // to offset for the first entry
FILE *fp;

if( (fp = fopen("/proc/stat", "r")) )
{
  while(fgets(str, sizeof str, fp))
  if( !memcmp(str, "cpu", 3) ) procCount++;
}

if ( procCount == -1) 
{ 
printf("Unable to get proc count. Defaulting to 2");
procCount=2;
}

printf("Proc Count:%d\n", procCount);
return 0;
}

Using /proc/cpuinfo:

#include<unistd.h>
#include<stdio.h>

int main(void)
{
char str[256];
int procCount = 0;
FILE *fp;

if( (fp = fopen("/proc/cpuinfo", "r")) )
{
  while(fgets(str, sizeof str, fp))
  if( !memcmp(str, "processor", 9) ) procCount++;
}

if ( !procCount ) 
{ 
printf("Unable to get proc count. Defaulting to 2");
procCount=2;
}

printf("Proc Count:%d\n", procCount);
return 0;
}

The same approach in shell using grep:

grep -c ^processor /proc/cpuinfo

Or

grep -c ^cpu /proc/stat # subtract 1 from the result
不一样的天空 2024-07-13 00:57:16

hwloc (http://www.open-mpi.org/projects/hwloc/) 值得一看。 虽然需要将另一个库集成到您的代码中,但它可以提供有关处理器的所有信息(核心数量、拓扑结构等)

hwloc (http://www.open-mpi.org/projects/hwloc/) is worth looking at. Though requires another library integration into your code but it can provide all the information about your processor (number of cores, the topology, etc.)

美煞众生 2024-07-13 00:57:16

据我所知,在 Linux 上最好的编程方式是使用

sysconf(_SC_NPROCESSORS_CONF)

sysconf(_SC_NPROCESSORS_ONLN)

这些不是标准的,但在我的 Linux 手册页中。

On linux the best programmatic way as far as I know is to use

sysconf(_SC_NPROCESSORS_CONF)

or

sysconf(_SC_NPROCESSORS_ONLN)

These aren't standard, but are in my man page for Linux.

星星的軌跡 2024-07-13 00:57:16

对于 Win32:

当 GetSystemInfo() 获取逻辑处理器的数量时,请使用
GetLogicalProcessorInformationEx()
获取物理处理器的数量。

For Win32:

While GetSystemInfo() gets you the number of logical processors, use
GetLogicalProcessorInformationEx()
to get the number of physical processors.

错々过的事 2024-07-13 00:57:16

OS X 替代方案:根据文档,前面描述的基于 [[NSProcessInfo processInfo] handlerCount] 的解决方案仅在 OS X 10.5.0 上可用。 对于早期版本的 OS X,请使用 Carbon 函数 MPProcessors()。

如果您是一名 Cocoa 程序员,请不要因为这是 Carbon 而感到害怕。 您只需要将 Carbon 框架添加到您的 Xcode 项目中,MPProcessors() 就可用了。

OS X alternative: The solution described earlier based on [[NSProcessInfo processInfo] processorCount] is only available on OS X 10.5.0, according to the docs. For earlier versions of OS X, use the Carbon function MPProcessors().

If you're a Cocoa programmer, don't be freaked out by the fact that this is Carbon. You just need to need to add the Carbon framework to your Xcode project and MPProcessors() will be available.

手长情犹 2024-07-13 00:57:16

也适用于 Linux:

#include<sys/sysinfo.h>
int nproc = get_nprocs();
int nproc_conf = get_nprocs_conf();
printf("processors available: %d, configured: %d\n", nproc, nproc_conf);

Also works on Linux:

#include<sys/sysinfo.h>
int nproc = get_nprocs();
int nproc_conf = get_nprocs_conf();
printf("processors available: %d, configured: %d\n", nproc, nproc_conf);
只怪假的太真实 2024-07-13 00:57:16
#include <stdint.h>

#if defined(__APPLE__) || defined(__FreeBSD__)
#include <sys/sysctl.h>

uint32_t num_physical_cores(void)
{
    uint32_t num_cores      = 0;
    size_t num_cores_len    = sizeof(num_cores);

    sysctlbyname("hw.physicalcpu", &num_cores, &num_cores_len, 0, 0);

    return num_cores;
}
#elif defined(__linux__)
#include <unistd.h>
#include <stdio.h>
uint32_t num_physical_cores(void)
{
    uint32_t lcores = 0, tsibs = 0;

    char buff[32];
    char path[64];

    for (lcores = 0;;lcores++) {
        FILE *cpu;

        snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%u/topology/thread_siblings_list", lcores);

        cpu = fopen(path, "r");
        if (!cpu) break;

        while (fscanf(cpu, "%[0-9]", buff)) {
            tsibs++;
            if (fgetc(cpu) != ',') break;
        }

        fclose(cpu);
    }

    return lcores / (tsibs / lcores);
}
#else
#error Unrecognized operating system
#endif

这应该返回系统上物理核心的数量。 这与大多数答案提供的逻辑核心数量不同。 如果您希望调整不执行阻塞 I/O 且不休眠的线程池的大小,那么您需要使用物理核心的数量,而不是逻辑(超线程)核心的数量。

该答案仅提供 Linux 和 BSD 的实现。

#include <stdint.h>

#if defined(__APPLE__) || defined(__FreeBSD__)
#include <sys/sysctl.h>

uint32_t num_physical_cores(void)
{
    uint32_t num_cores      = 0;
    size_t num_cores_len    = sizeof(num_cores);

    sysctlbyname("hw.physicalcpu", &num_cores, &num_cores_len, 0, 0);

    return num_cores;
}
#elif defined(__linux__)
#include <unistd.h>
#include <stdio.h>
uint32_t num_physical_cores(void)
{
    uint32_t lcores = 0, tsibs = 0;

    char buff[32];
    char path[64];

    for (lcores = 0;;lcores++) {
        FILE *cpu;

        snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%u/topology/thread_siblings_list", lcores);

        cpu = fopen(path, "r");
        if (!cpu) break;

        while (fscanf(cpu, "%[0-9]", buff)) {
            tsibs++;
            if (fgetc(cpu) != ',') break;
        }

        fclose(cpu);
    }

    return lcores / (tsibs / lcores);
}
#else
#error Unrecognized operating system
#endif

This should return the number of physical cores on the system. This is different than the number of logical cores which most of these answers provide. If you're looking to size a thread pool which performs no blocking I/O, and doesn't sleep, then you want to use the number of physical cores, not the number of logical (hyper threading) cores.

This answer only provides implementations for Linux and the BSDs.

自此以后,行同陌路 2024-07-13 00:57:16

你也可以在.net中使用WMI,但是你依赖于运行的wmi服务
有时它可以在本地运行,但当相同的代码在服务器上运行时就会失败。
我认为这是一个命名空间问题,与您正在读取其值的“名称”有关。

you can use WMI in .net too but you're then dependent on the wmi service running
etc. Sometimes it works locally, but then fail when the same code is run on servers.
I believe that's a namespace issue, related to the "names" whose values you're reading.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文