在C#.net中检查Os是32位还是64位

问题描述:



如何在C#.net中检查Os是32位还是64位?这是一个Windows应用程序吗?请该如何完成这个任务。

Hi,
How to check Os is 32bit or 64bit in C#.net?It is an windows application?Please how to achieve this task.

使用System.Environment.GetEnvironmentVariable()方法

use System.Environment.GetEnvironmentVariable() method
Console.WriteLine(System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE").ToString());




and you can use as

 if (Environment.Is64BitOperatingSystem) 
   {
     return Environment.GetEnvironmentVariable("SysWOW64"); 
   }
  else
   {
      return Environment.GetEnvironmentVariable("system32");       
   }


非常简单。

您可以使用IntPtr大小进行检查。对于32位操作系统,IntPtr sixe为4,对于64位操作系统为8,

it is very simple.
You can check using IntPtr size. IntPtr sixe is 4 for 32 BIT OS and 8 for 64 BIT OS
if (IntPtr.Size == 8)
// 64Bit
else
// 32bit









使用intPtr和process我们可以搜索OS64位。使用以下代码





OR

Using intPtr and process we can search is OS64 bit. use the following code

[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
2 [return: MarshalAs(UnmanagedType.Bool)]
3 public static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool lpSystemInfo);
4
5 public bool Is64Bit()
6 {
7     bool retVal;
8
9     IsWow64Process(Process.GetCurrentProcess().Handle, out retVal);
10
11     return retVal;


有关多种解决方案,请参阅此提示/技巧:

32位或64位操作系统?? [ ^ ]
See this Tip/Trick for several solutions:
32-Bit or 64-bit OS ??[^]