Skip to content

Commit 144f5c5

Browse files
committedJul 4, 2023
Fix CPU detection on Windows when wmic is not available via PATH, fix #2271
1 parent a6edfe6 commit 144f5c5

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
2+
using System.IO;
23
using BenchmarkDotNet.Helpers;
3-
using JetBrains.Annotations;
44

55
namespace BenchmarkDotNet.Portability.Cpu
66
{
@@ -10,17 +10,21 @@ namespace BenchmarkDotNet.Portability.Cpu
1010
/// </summary>
1111
internal static class WmicCpuInfoProvider
1212
{
13-
internal static readonly Lazy<CpuInfo> WmicCpuInfo = new Lazy<CpuInfo>(Load);
13+
internal static readonly Lazy<CpuInfo> WmicCpuInfo = new (Load);
14+
15+
private const string DefaultWmicPath = @"C:\Windows\System32\wbem\WMIC.exe";
1416

1517
private static CpuInfo? Load()
1618
{
1719
if (RuntimeInformation.IsWindows())
1820
{
19-
string argList = $"{WmicCpuInfoKeyNames.Name}, {WmicCpuInfoKeyNames.NumberOfCores}, {WmicCpuInfoKeyNames.NumberOfLogicalProcessors}, {WmicCpuInfoKeyNames.MaxClockSpeed}";
20-
string content = ProcessHelper.RunAndReadOutput("wmic", $"cpu get {argList} /Format:List");
21+
const string argList = $"{WmicCpuInfoKeyNames.Name}, {WmicCpuInfoKeyNames.NumberOfCores}, " +
22+
$"{WmicCpuInfoKeyNames.NumberOfLogicalProcessors}, {WmicCpuInfoKeyNames.MaxClockSpeed}";
23+
string wmicPath = File.Exists(DefaultWmicPath) ? DefaultWmicPath : "wmic";
24+
string content = ProcessHelper.RunAndReadOutput(wmicPath, $"cpu get {argList} /Format:List");
2125
return WmicCpuInfoParser.ParseOutput(content);
2226
}
2327
return null;
2428
}
2529
}
26-
}
30+
}

0 commit comments

Comments
 (0)
Please sign in to comment.