========================================================================
How to detect your platform is 32 bits or 64 bits? It has an easy way to judge your platform in C#. We can use the size of IntPtr to know our platform. Here is some document on MSDN about IntPtr as below.
The IntPtr type is designed to be an integer whose size is platform-specific. That is, an instance of this type is expected to be 32-bits on 32-bit hardware and operating systems, and 64-bits on 64-bit hardware and operating systems.
The IntPtr type can be used by languages that support pointers, and as a common means of referring to data between languages that do and do not support pointers.
IntPtr objects can also be used to hold handles. For example, instances of IntPtr are used extensively in theSystem.IO.FileStream class to hold file handles.
The IntPtr type is CLS-compliant, while the UIntPtr type is not. Only the IntPtr type is used in the common language runtime. The UIntPtr type is provided mostly to maintain architectural symmetry with the IntPtr type.
This type implements the ISerializable interface.
We use an sample to detect the platform-specific.
1. New a Windows Forms Application project and add below codes.
2. We run the application in x86 platform. The result shows in below.
3. We run the application in x64 platform. The result shows in below.
Reference: MSDN
中文
========================================================================
判斷系統為 32bit 還是 64bit 最簡單的方式,就是使用 IntPtr 去計算 Size 為多少,在 x86 的環境,回傳值為 4,在 x64 的環境,回傳值為 8,以下為簡單的範例程式碼:
1. 新創一個新的 Windows Forms Application 專案,並在專案中添加以下程式碼:
2. 當程式執行於 x86 平台時,執行結果如下:
3. 當程式執行於 x64 平台時,執行結果如下:
沒有留言:
張貼留言