使用C#,如何获取我的计算机是64位还是32位?

13

使用C#,我想创建一个方法,以返回我的计算机是64位还是32位。

有没有人知道如何做到这一点?


2
可能是重复的问题:如何以编程方式确定我的处理器类型? - user195488
@MyrS - 根据使用的 .Net 版本不同,有不同的方法可以实现这一点。在早期的 .Net 平台版本中,Is64BitOperatingSystem() 不可用。 - Jon Raynor
7个回答


6
System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")

请参考这个问题。

4
这里是需要翻译的内容:

这里是:

如何使用.NET检测Windows 64位平台?

摘录:

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;
    }
}

因为这只是一种极其复杂的方式来完成一件非常简单的事情,我认为这很棒,所以我点了个赞(当然,只要它不出现在我的任何生产代码中)。System.Environment.Is64BitOperatingSystem 可以用一行代码完成相同的操作。 - Jarrett Meyer
1
为什么要简单化,当你可以把它复杂化呢? - GrandMarquis
2
@Jarrett - 并非每个人都有使用较新版本的 .Net 的奢侈条件,因此有时您必须进行 PInvoking。 - Jon Raynor
谢谢!Is64BitOperatingSystem() 对我来说很好用 :) 我猜我有最新的 .Net 平台版本。 - Slt

1

我已经为我的一个项目编写了这个代码(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);

1
    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;
        }

0

您可以使用IntPtr检查大小。IntPtr大小在32位操作系统上为4,在64位操作系统上为8。

/// <summary>Is64s the bit operating system.</summary>
/// <returns></returns>
if (IntPtr.Size == 8)
    // 64Bit
else
    // 32bit

类型:System.Int32

在此进程中的指针或句柄大小,以字节为单位度量。此属性的值在32位进程中为4,而在64位进程中为8。您可以通过在使用C#Visual Basic编译器编译代码时设置/platform开关来定义进程类型。


0

您可以使用:

System.Runtime.InteropServices.RuntimeInformation.OSArchitecture

返回一个枚举。文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接