Ani*_*oel 19 c# architecture cpu identify
我想检查用户正在运行的CPU架构,是i386还是X64或AMD64.我想用C#做.我知道我可以尝试WMI或注册表.除了这两个之外还有其他方式吗?我的项目面向.NET 2.0!
Meh*_*ari 27
你也可以试试(只有在没有被操纵的情况下才有效):
System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
Run Code Online (Sandbox Code Playgroud)
小智 23
我在这里得到的是检查32对64位操作系统.评分最高的答案是查看当前流程的设置.找不到答案后,我找到了以下设置.希望这对你有用.
bool is64 = System.Environment.Is64BitOperatingSystem
Run Code Online (Sandbox Code Playgroud)
cub*_*e45 12
我知道这个问题来自过去,但截至 2017 年,现在有一个简单的方法来了解当前进程的架构,在 .net 标准中:
System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture
Run Code Online (Sandbox Code Playgroud)
返回的值是 X86、X64、ARM、ARM64 之一,并给出运行它的进程OSArchitecture的体系结构。而是返回已安装操作系统的体系结构。
链接到文档(虽然很没用......):
RuntimeInformation.ProcessArchitecture:https ://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.runtimeinformation.processarchitecture ? view = netstandard-1.4
架构枚举:https : //docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.architecture?view=netstandard-1.4
Sim*_*ier 10
这是一段似乎有用的代码(基于P/Invoke):
public static ProcessorArchitecture GetProcessorArchitecture()
{
SYSTEM_INFO si = new SYSTEM_INFO();
GetNativeSystemInfo(ref si);
switch (si.wProcessorArchitecture)
{
case PROCESSOR_ARCHITECTURE_AMD64:
return ProcessorArchitecture.Amd64;
case PROCESSOR_ARCHITECTURE_IA64:
return ProcessorArchitecture.IA64;
case PROCESSOR_ARCHITECTURE_INTEL:
return ProcessorArchitecture.X86;
default:
return ProcessorArchitecture.None; // that's weird :-)
}
}
Run Code Online (Sandbox Code Playgroud)
同
[DllImport("kernel32.dll")]
private static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSystemInfo);
private const int PROCESSOR_ARCHITECTURE_AMD64 = 9;
private const int PROCESSOR_ARCHITECTURE_IA64 = 6;
private const int PROCESSOR_ARCHITECTURE_INTEL = 0;
[StructLayout(LayoutKind.Sequential)]
private struct SYSTEM_INFO
{
public short wProcessorArchitecture;
public short wReserved;
public int dwPageSize;
public IntPtr lpMinimumApplicationAddress;
public IntPtr lpMaximumApplicationAddress;
public IntPtr dwActiveProcessorMask;
public int dwNumberOfProcessors;
public int dwProcessorType;
public int dwAllocationGranularity;
public short wProcessorLevel;
public short wProcessorRevision;
}
Run Code Online (Sandbox Code Playgroud)
请注意,此代码重用现有CLR的ProcessorArchitecture枚举,并支持.NET framework 2及更高版本.
最后,解决C#中当前运行的CLR运行时的平台/处理器架构的最短技巧是:
PortableExecutableKinds peKind;
ImageFileMachine machine;
typeof(object).Module.GetPEKind(out peKind, out machine);
Run Code Online (Sandbox Code Playgroud)
这里Module.GetPEKind返回一个ImageFileMachine枚举,它存在于.NET v2之后:
public enum ImageFileMachine
{
I386 = 0x014C,
IA64 = 0x0200,
AMD64 = 0x8664,
ARM = 0x01C4 // new in .NET 4.5
}
Run Code Online (Sandbox Code Playgroud)
为什么不使用new AssemblyName(fullName)或typeof(object).Assembly.GetName()?
那么HACK在ASP.NET MVC源代码中有这个注释(从1.0开始):
private static string GetMvcVersionString() {
// DevDiv 216459:
// This code originally used Assembly.GetName(), but that requires FileIOPermission, which isn't granted in
// medium trust. However, Assembly.FullName *is* accessible in medium trust.
return new AssemblyName(typeof(MvcHttpHandler).Assembly.FullName).Version.ToString(2);
}
Run Code Online (Sandbox Code Playgroud)
看到他们为自己使用一些隐藏的技巧.遗憾的是,AssemblyName构造函数没有ProcessorArchitecture适当地设置字段,它只None适用于任何新的AssemblyName.
因此,对于未来的读者,我建议您使用带有ImageFileMachine的丑陋的GetPEKind!
笔记:
这个怎么样?
switch (typeof(string).Assembly.GetName().ProcessorArchitecture) {
case System.Reflection.ProcessorArchitecture.X86:
// '$(Platform)' == 'x86'
break;
case System.Reflection.ProcessorArchitecture.Amd64:
// '$(Platform)' == 'x64'
break;
case System.Reflection.ProcessorArchitecture.MSIL:
// MacBook Pro with M1 Pro (from comment)
break;
case System.Reflection.ProcessorArchitecture.None:
// ASP.NET Core Blazor WebAssembly
break;
case System.Reflection.ProcessorArchitecture.Arm:
// Unknown
break;
}
Run Code Online (Sandbox Code Playgroud)
但case *.Arm:尚未测试。