How to detect Windows 64-bit platform with .NET?

前端 未结 29 2693
野性不改
野性不改 2020-11-22 04:53

In a .NET 2.0 C# application I use the following code to detect the operating system platform:

string os_platform = System.Environment.OSVersion.Platform.ToS         


        
相关标签:
29条回答
  • 2020-11-22 05:24

    Include the following code into a class in your project:

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

    Use it like so:

    string Architecture = "This is a " + GetBit() + "bit machine";
    
    0 讨论(0)
  • 2020-11-22 05:24

    Given that the accepted answer is very complex. There are simpler ways. Mine is a variation of alexandrudicu's anaswer. Given that 64-bit windows install 32-bit applications in Program Files (x86) you can check if that folder exists, using environment variables (to make up for different localizations)

    e.g.

    private bool Is64BitSystem
    {
       get
       {
          return Directory.Exists(Environment.ExpandEnvironmentVariables(@"%PROGRAMFILES(X86)%"));
       }
    }
    

    This for me is faster and simpler. Given that I also wish to access a specific path under that folder based on OS version.

    0 讨论(0)
  • 2020-11-22 05:25

    Use these two environment variables (pseudo code):

    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
            }
    

    Refer to the blog post HOWTO: Detect Process Bitness.

    0 讨论(0)
  • 2020-11-22 05:28

    I used this check with success on many operating systems:

    private bool Is64BitSystem
    {
       get
       {
          return Directory.Exists(Environment.ExpandEnvironmentVariables(@"%windir%\SysWOW64"));
       }
    }
    

    This folder is always named "SysWOW64", no matter of the language of the operating system. This works for .NET Framework 1.1 or above.

    0 讨论(0)
  • 2020-11-22 05:28

    I'm using the followin code. Note: It's made for an AnyCPU project.

        public static bool Is32bitProcess(Process proc) {
            if (!IsThis64bitProcess()) return true; // We're in 32-bit mode, so all are 32-bit.
    
            foreach (ProcessModule module in proc.Modules) {
                try {
                    string fname = Path.GetFileName(module.FileName).ToLowerInvariant();
                    if (fname.Contains("wow64")) {
                        return true;
                    }
                } catch {
                    // What on earth is going on here?
                }
            }
            return false;
        }
    
        public static bool Is64bitProcess(Process proc) {
            return !Is32bitProcess(proc);
        }
    
        public static bool IsThis64bitProcess() {
            return (IntPtr.Size == 8);
        }
    
    0 讨论(0)
  • 2020-11-22 05:28

    Just see if the "C:\Program Files (x86)" exists. If not, then you are on a 32 bit OS. If it does, then the OS is 64 bit (Windows Vista or Windows 7). It seems simple enough...

    0 讨论(0)
提交回复
热议问题