回答
System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
看到this问题。
这就是:
How to detect Windows 64-bit platform with .NET?
报价:
bool is64BitProcess = (IntPtr.Size == 8);
bool is64BitOperatingSystem = is64BitProcess || InternalCheckIsWow64();
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWow64Process(
[In] IntPtr hProcess,
[Out] out bool wow64Process
);
public static bool InternalCheckIsWow64()
{
if ((Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1) ||
Environment.OSVersion.Version.Major >= 6)
{
using (Process p = Process.GetCurrentProcess())
{
bool retVal;
if (!IsWow64Process(p.Handle, out retVal))
{
return false;
}
return retVal;
}
}
else
{
return false;
}
}
Upvoted,只是因为这是一个非常复杂的方式来做一个非常简单的事情,我认为这太棒了。 (也就是说,只要它永远不会出现在我的任何生产代码中。)'System.Environment.Is64BitOperatingSystem'将在同一行中执行相同的操作。 –
为什么使它变得容易,当你可以复杂它? – jDourlens
@Jarrett - 并不是每个人都可以使用.Net的更高版本来做这件事,所以有时候你会去PInvoking。 –
我有这个编码我的一个项目(C#VS 2005)。
//DLL Imports
using System.Runtime.InteropServices;
/// <summary>
/// The function determines whether the current operating system is a
/// 64-bit operating system.
/// </summary>
/// <returns>
/// The function returns true if the operating system is 64-bit;
/// otherwise, it returns false.
/// </returns>
public static bool Is64BitOperatingSystem()
{
if (IntPtr.Size == 8) // 64-bit programs run only on Win64
{
return true;
}
else // 32-bit programs run on both 32-bit and 64-bit Windows
{
// Detect whether the current process is a 32-bit process
// running on a 64-bit system.
bool flag;
return ((DoesWin32MethodExist("kernel32.dll", "IsWow64Process") &&
IsWow64Process(GetCurrentProcess(), out flag)) && flag);
}
}
/// <summary>
/// The function determins whether a method exists in the export
/// table of a certain module.
/// </summary>
/// <param name="moduleName">The name of the module</param>
/// <param name="methodName">The name of the method</param>
/// <returns>
/// The function returns true if the method specified by methodName
/// exists in the export table of the module specified by moduleName.
/// </returns>
static bool DoesWin32MethodExist(string moduleName, string methodName)
{
IntPtr moduleHandle = GetModuleHandle(moduleName);
if (moduleHandle == IntPtr.Zero)
{
return false;
}
return (GetProcAddress(moduleHandle, methodName) != IntPtr.Zero);
}
[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentProcess();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern IntPtr GetModuleHandle(string moduleName);
[DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr GetProcAddress(IntPtr hModule,
[MarshalAs(UnmanagedType.LPStr)]string procName);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWow64Process(IntPtr hProcess, out bool wow64Process);
public static string t2or64()
{
string t2s4;
bool os = System.Environment.Is64BitOperatingSystem;
int x = 0;
if (os == true)
{
x = 64;
}
else
{
x = 32;
}
t2s4 = Convert.ToString(x);
return t2s4;
}
您可以检查使用IntPtr大小。 IntPtr的大小为4 32位操作系统和8的64位操作系统
/// <summary>Is64s the bit operating system.</summary>
/// <returns></returns>
if (IntPtr.Size == 8)
// 64Bit
else
// 32bit
类型:System.Int32
一个指针的大小或在此过程中处理,以字节为单位。该属性的值是4在32位处理中和8在64位处理中。当您使用C#
和3210编译器编译代码时,您可以通过设置/platform
开关来定义过程类型。
- 1. 如何使用c程序找到我的机器是16位还是32位或64位
- 2. 无论系统是32位还是64位,int都是32位还是64位?
- 3. 如何判断我写的程序是32位还是64位?
- 4. 如何检查我的Perl安装是32位还是64位?
- 5. 如何判断我的cygwin安装是32位还是64位?
- 6. 如何找到它是32位还是64位? Tomcat
- 7. 我应该在新机器上使用Eclipse 32位还是64位?
- 8. 无论系统是32位还是64位,uint均为32位?
- 9. 如何检测Cocoa应用程序是32位还是64位?
- 10. 如何判断应用程序是64位还是32位?
- 11. 如何检查Linux文件(.so)是32位还是64位从Windows机器
- 12. 使用WMI,我如何确定远程进程是32位还是64位?
- 13. 我应该使用32位还是64位JDK?
- 14. 我应该使用Python 32位还是Python 64位
- 15. 如何检查安装的wxPython是32位还是64位?
- 16. 如何检查安装的Anaconda是32位还是64位?
- 17. 如何检测,如果CPU是32位还是64位
- 18. 我怎么知道gcc的版本是64位还是32位?
- 19. 在32位和64位机器中总是长64位
- 20. 是否可以使用PHP知道我的平台是64位还是32位?
- 21. 如何确定System.Diagnostics.Process是32位还是64位?
- 22. 如何知道libpython27.a是32位还是64位?
- 23. 如何检查WebSphere是32位还是64位?
- 24. 检查处理器是32位还是64位
- 25. 是为32位还是64位机器编译的共享对象?
- 26. 如何知道安装的jvm是32位还是64位使用javascript
- 27. 迁移C++代码32位机器到64位机器
- 28. 如何确定我的操作系统是32位还是64位?
- 29. 如何判断我的现有Amazon EC2实例是32位还是64位?
- 30. 如何查找我的Amazon EC2实例是32位还是64位?
可能重复[如何以编程方式确定我的处理器类型?](http://stackoverflow.com/questions/1020581/how-can-i-programmatically-determine-my-processor-type) – 2011-08-11 19:50:16
@MyrS - 根据使用哪个.Net版本,有不同的方法来执行此操作。早期的.Net平台版本不支持Is64BitOperatingSystem()。 –