在.NET 2.0 c#应用程序中,我使用以下代码来检测操作系统平台:

string os_platform = System.Environment.OSVersion.Platform.ToString();

返回“Win32NT”。问题是即使在Windows Vista 64位上运行,它也会返回“Win32NT”。

有没有其他方法来知道正确的平台(32位或64位)?

注意,当在Windows 64位上作为32位应用程序运行时,它也应该检测64位。


当前回答

在你的项目中包含以下代码:

    [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 int GetBit()
    {
        int MethodResult = "";
        try
        {
            int Architecture = 32;

            if ((Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1) || Environment.OSVersion.Version.Major >= 6)
            {
                using (Process p = Process.GetCurrentProcess())
                {
                    bool Is64Bit;

                    if (IsWow64Process(p.Handle, out Is64Bit))
                    {
                        if (Is64Bit)
                        {
                            Architecture = 64;

                        }

                    }

                }

            }

            MethodResult = Architecture;

        }
        catch //(Exception ex)
        {
            //ex.HandleException();
        }
        return MethodResult;
    }

像这样使用它:

string Architecture = "This is a " + GetBit() + "bit machine";

其他回答

使用以下两个环境变量(伪代码):

if (PROCESSOR_ARCHITECTURE = x86 &&
    isDefined(PROCESSOR_ARCHITEW6432) &&
    PROCESSOR_ARCHITEW6432 = AMD64) {

    //64 bit OS
}
else
    if (PROCESSOR_ARCHITECTURE = AMD64) {
        //64 bit OS
    }
    else
        if (PROCESSOR_ARCHITECTURE = x86) {
            //32 bit OS
        }

请参阅博客文章HOWTO:检测进程bit。

我使用:

Dim drivelet As String = Application.StartupPath.ToString
If Directory.Exists(drivelet(0) & ":\Program Files (x86)") Then
    MsgBox("64bit")
Else
    MsgBox("32bit")
End if

这将获取应用程序启动的路径,以防您将其安装在计算机上的不同位置。此外,你也可以只使用一般的C:\路径,因为99.9%的计算机都在C:\中安装了Windows。

使用此命令获取已安装的Windows体系结构:

string getOSArchitecture()
{
    string architectureStr;
    if (Directory.Exists(Environment.GetFolderPath(
                           Environment.SpecialFolder.ProgramFilesX86))) {
        architectureStr ="64-bit";
    }
    else {
        architectureStr = "32-bit";
    }
    return architectureStr;
}

对不起,是VB的代码。NET,但是很容易移植到c#。 它可以在Win7, x64和.Net Framework 4.8上正常工作:

Dim r As String = ""
Using searcher As ManagementObjectSearcher = New System.Management.ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem")
    Dim Information As ManagementObjectCollection = searcher.Get()
    If Information IsNot Nothing Then
        For Each obj As ManagementObject In Information
            r = obj("Caption").ToString() & _
            " - " & _
            obj("OSArchitecture").ToString() ' <--- !!! "64-bit" shown here
        Next
    End If
    MessageBox.Show(r)
End Using

试试这个:

Environment.Is64BitOperatingSystem

Environment.Is64BitProcess