2011-08-11 88 views
12

使用C#,我想創建一個方法來重新調整我的機器是64位還是32位。使用C#,如何得到我的機器是64位還是32位?

有沒有人知道如何做到這一點?

+2

可能重複[如何以編程方式確定我的處理器類型?](http://stackoverflow.com/questions/1020581/how-can-i-programmatically-determine-my-processor-type) – 2011-08-11 19:50:16

+0

@MyrS - 根據使用哪個.Net版本,有不同的方法來執行此操作。早期的.Net平臺版本不支持Is64BitOperatingSystem()。 –

回答

6
System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") 

看到this問題。

4

這就是:

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

Upvoted,只是因爲這是一個非常複雜的方式來做一個非常簡單的事情,我認爲這太棒了。 (也就是說,只要它永遠不會出現在我的任何生產代碼中。)'System.Environment.Is64BitOperatingSystem'將在同一行中執行相同的操作。 –

+1

爲什麼使它變得容易,當你可以複雜它? – jDourlens

+2

@Jarrett - 並不是每個人都可以使用.Net的更高版本來做這件事,所以有時候你會去PInvoking。 –

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

您可以檢查使用IntPtr大小。 IntPtr的大小爲4 32位操作系統和8的64位操作系統

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

類型:System.Int32

一個指針的大小或在此過程中處理,以字節爲單位。該屬性的值是4在32位處理中8在64位處理中。當您使用C#和​​3210編譯器編譯代碼時,您可以通過設置/platform開關來定義過程類型。

相關問題