Подтвердить что ты не робот

Используя С#, как получить, является ли моя машина 64-битной или 32-битной?

Используя С#, я хотел бы создать метод, который возвращает, является ли моя машина 64 или 32-разрядной.

Есть ли кто-нибудь, кто знает, как это сделать?

4b9b3361

Ответ 2

System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")

см. этот вопрос.

Ответ 3

Вот он:

Как определить 64-разрядную платформу Windows с .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;
    }
}

Ответ 4

У меня это закодировано для одного из моих проектов (С# 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 размер. Размер 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-разрядном процессе. Вы можете определить тип процесса, установив переключатель /platform при компиляции кода с помощью компиляторов C# и Visual Basic.