我的C#应用程序如何检查特定应用程序/进程(注意:当前进程)是否在32位或64位模式下运行?
例如,我可能想要按名称查询特定进程,即“abc.exe”,或者根据进程ID号查询。
答案 0 :(得分:166)
我见过的一个更有趣的方法是:
if (IntPtr.Size == 4)
{
// 32-bit
}
else if (IntPtr.Size == 8)
{
// 64-bit
}
else
{
// The future is now!
}
要确定在64位仿真器(WOW64)中是否正在运行OTHER进程,请使用以下代码:
namespace Is64Bit
{
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
internal static class Program
{
private static void Main()
{
foreach (var p in Process.GetProcesses())
{
try
{
Console.WriteLine(p.ProcessName + " is " + (p.IsWin64Emulator() ? string.Empty : "not ") + "32-bit");
}
catch (Win32Exception ex)
{
if (ex.NativeErrorCode != 0x00000005)
{
throw;
}
}
}
Console.ReadLine();
}
private static bool IsWin64Emulator(this Process process)
{
if ((Environment.OSVersion.Version.Major > 5)
|| ((Environment.OSVersion.Version.Major == 5) && (Environment.OSVersion.Version.Minor >= 1)))
{
bool retVal;
return NativeMethods.IsWow64Process(process.Handle, out retVal) && retVal;
}
return false; // not on 64-bit Windows Emulator
}
}
internal static class NativeMethods
{
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool IsWow64Process([In] IntPtr process, [Out] out bool wow64Process);
}
}
答案 1 :(得分:136)
如果您使用的是.Net 4.0,那么它就是当前流程的单行代码:
Environment.Is64BitProcess
请参阅 Environment.Is64BitProcessProperty (MSDN)。
答案 2 :(得分:16)
所选答案不正确,因为它没有做出要求。它检查进程是否是在x64 OS上运行的x86进程;所以它将返回" false"用于在x86 OS上运行的x64进程或在x86 OS上运行的x86进程 此外,它没有正确处理错误。
这是一个更正确的方法:
internal static class NativeMethods
{
// see https://msdn.microsoft.com/en-us/library/windows/desktop/ms684139%28v=vs.85%29.aspx
public static bool Is64Bit(Process process)
{
if (!Environment.Is64BitOperatingSystem)
return false;
// if this method is not available in your version of .NET, use GetNativeSystemInfo via P/Invoke instead
bool isWow64;
if (!IsWow64Process(process.Handle, out isWow64))
throw new Win32Exception();
return !isWow64;
}
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWow64Process([In] IntPtr process, [Out] out bool wow64Process);
}
答案 3 :(得分:10)
您可以检查指针的大小,以确定它是32位还是64位。
int bits = IntPtr.Size * 8;
Console.WriteLine( "{0}-bit", bits );
Console.ReadLine();
答案 4 :(得分:4)
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool lpSystemInfo);
public static bool Is64Bit()
{
bool retVal;
IsWow64Process(Process.GetCurrentProcess().Handle, out retVal);
return retVal;
}
答案 5 :(得分:1)
这是一行检查。
bool is64Bit = IntPtr.Size == 8;
答案 6 :(得分:0)
我喜欢用这个:
string e = Environment.Is64BitOperatingSystem
这样,如果我需要找到或验证文件,我可以轻松写出:
string e = Environment.Is64BitOperatingSystem
// If 64 bit locate the 32 bit folder
? @"C:\Program Files (x86)\"
// Else 32 bit
: @"C:\Program Files\";