【问题标题】:Using C#, how to get whether my machine is 64bit or 32bit?使用 C#,如何获取我的机器是 64 位还是 32 位?
【发布时间】:2011-10-25 07:20:30
【问题描述】:

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

有人知道怎么做吗?

【问题讨论】:

标签: c# operating-system 32bit-64bit


【解决方案1】:
【解决方案2】:
System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")

请参阅this 问题。

【讨论】:

    【解决方案3】:

    这里是:

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

    【讨论】:

    • 赞成,因为这是一种非常复杂的方法来做一件非常简单的事情,我认为这太棒了。 (也就是说,只要它从未出现在我的任何生产代码中。)System.Environment.Is64BitOperatingSystem 将在一行中执行相同的操作。
    • 既然可以复杂化,为什么要让它变得简单?
    • @Jarrett - 不是每个人都可以使用更高版本的 .Net 来做这件事,所以有时你不得不去 PInvoking。
    • 谢谢! Is64BitOperatingSystem() 对我来说很好 :) 我想我有最新的 .Net 平台版本。
    【解决方案4】:

    我为我的一个项目(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);
    

    【讨论】:

      【解决方案5】:
          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;
              }
      

      【讨论】:

        【解决方案6】:

        您可以使用IntPtr 大小进行检查。 32 位操作系统的 IntPtr 大小为 4,64 位操作系统的 IntPtr 大小为 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 开关来定义进程类型。

        【讨论】:

          猜你喜欢
          • 2011-01-24
          • 1970-01-01
          • 2011-04-19
          • 1970-01-01
          • 2011-04-20
          • 1970-01-01
          • 2016-02-06
          • 2010-10-23
          • 2012-05-26
          相关资源
          最近更新 更多