我的程序如何确定它是在 32 位还是 64 位 Windows 上运行?

发布于 2024-09-02 03:29:11 字数 396 浏览 2 评论 0原文

这是我的问题。确定您的应用程序运行在哪种位架构上的最佳方法是什么?

我想要做什么:在 64 位服务器上,我希望我的应用程序读取 64 位数据源(存储在 reg key Software\Wow6432Node\ODBC\ODBC.INI\ODBC Data Sources 中),如果是 32 位,我想读取 32位数据源(即从 Software\ODBC\ODBC.INI\ODBC 数据源读取)。

我可能没有抓住重点,但我不想关心我的应用程序运行的模式。我只是想知道操作系统是 32 位还是 64 位。

[System.Environment.OSVersion.Platform 似乎并不适合我。它在我的本地 xp 机器和 win2k8 64 位服务器上返回 Win32NT(即使我的所有项目都设置为针对“任何 cpu”)]

Here's my question. What is the best way to determine what bit architecture your app is running on?

What I am looking to do: On a 64 bit server I want my app to read 64 bit datasources (stored in reg key Software\Wow6432Node\ODBC\ODBC.INI\ODBC Data Sources) and if its 32 bit I want to read 32 bit datasources, (i.e. Read from Software\ODBC\ODBC.INI\ODBC Data Sources).

I might be missing the point, but I don't want to care what mode my app is running in. I simply want to know if the OS is 32 or 64 bit.

[System.Environment.OSVersion.Platform doesn't seem to be cutting it for me. Its returning Win32NT on my local xp machine and on a win2k8 64 bit server (even when all my projects are set to target 'any cpu')]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

调妓 2024-09-09 03:29:11

通常你甚至不应该担心这个。在 64 位平台上运行 32 位应用程序时,系统会自动将注册表查询重定向到 Software\Wow6432Node

You shouldn't even worry about this, normally. The system automatically redirects registry queries to Software\Wow6432Node when running a 32-bit app on a 64-bit platform.

青春如此纠结 2024-09-09 03:29:11

尝试使用属性 Environment.Is64BitOperatingSystem。这是.Net 4.0中新增的一项,专门用于检查操作系统的类型。

Try the property Environment.Is64BitOperatingSystem. This is a new one added in .Net 4.0 specifically for the purpose of checking the type of the operating system.

心如狂蝶 2024-09-09 03:29:11

简单、安全、框架版本无关的解决方案,无需访问注册表:

Console.WriteLine(
    "Is 64-bit? {0}",
    (
        System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)) == sizeof(Int64)
            ? "Yes" 
            : "No"
    )
);

Simple, safe, framework version agnostic solution without going to the registry:

Console.WriteLine(
    "Is 64-bit? {0}",
    (
        System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)) == sizeof(Int64)
            ? "Yes" 
            : "No"
    )
);
り繁华旳梦境 2024-09-09 03:29:11

您不应该直接阅读 Wow6432Node。使用 RegistryView作为 64 位应用程序运行时指定 32 位视图。

You should not read Wow6432Node directly. Use RegistryView to specify a 32-bit view when running as a 64-bit app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文