检测cpu架构的正确方法? [英] Right way to detect cpu architecture?

查看:36
本文介绍了检测cpu架构的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测用于安装 x86 msi 或 x64 msi 文件的正确 CPU 架构.

I'm attempting to detect the right cpu architecture for installing either a x86 msi or x64 msi file.

如果我是对的,对于 msi,我需要 os cpu 架构

If I'm right, for the msi I need the os cpu architecture

我不完全确定我的方法是否正确,因为我无法对其进行测试.你怎么看?

I'm not totally sure if my way is right because I can't test it. What do you think?

private static string GetOSArchitecture()
    {
        string arch = System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
        string archWOW = System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432");
        if(archWOW != null && archWOW != "" && archWOW.Contains("64"))
            return "x64";
        if(arch.Contains("86"))
            return "x86";
        if (arch.Contains("64"))
            return "x64";
        return "";
    }

推荐答案

您可以 P/Invoke 到 GetNativeSystemInfo,它将给出操作系统的 CPU 架构,即使在 64 位操作系统上的 32 位进程中也是如此.

You could P/Invoke to GetNativeSystemInfo, which will give the CPU architecture of the OS, even from within a 32-bit process on a 64-bit OS.

这篇关于检测cpu架构的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆