1. 程式人生 > >c#判斷操作系統是32位還是64位

c#判斷操作系統是32位還是64位

ping interop 64位 lin last 直接 run 遇到 roc

做一個c#項目時,遇到要獲取操作系統位數的問題,在網上找了幾個小時,都沒有找到比較完整的解決方案。話不多說,直接上可以運行的代碼(簡單、粗暴)

using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;

internal static class Win32Native
    {
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
        [DllImport("kernel32.dll
", BestFitMapping = false, CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr GetModuleHandle(string moduleName); [DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)] private static extern
IntPtr GetProcAddress(IntPtr hModule, string methodName); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool IsWow64Process([In] IntPtr hSourceProcessHandle, [MarshalAs(UnmanagedType.Bool)] out bool isWow64); [DllImport(
"kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] internal static extern IntPtr GetCurrentProcess(); internal static bool DoesWin32MethodExist(string moduleName, string methodName) { IntPtr moduleHandle = Win32Native.GetModuleHandle(moduleName); if (moduleHandle == IntPtr.Zero) { return false; } IntPtr procAddress = Win32Native.GetProcAddress(moduleHandle, methodName); return procAddress != IntPtr.Zero; } public static bool Is64BitOperatingSystem { get { bool flag=default(bool); return (Win32Native.DoesWin32MethodExist("kernel32.dll", "IsWow64Process") && Win32Native.IsWow64Process(Win32Native.GetCurrentProcess(), out flag)) & flag; } } }

c#判斷操作系統是32位還是64位