平台目标 x86 和任何 CPU
我对简单的代码有一个小问题。该代码可以在“x86”模式下正常工作,但不能在“任何CPU”模式下正常工作,也许可以在“x86”模式上运行一个类,在“任何CPU”模式下运行另一个类?代码:
namespace Software_Info_v1._0
{
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
public class Adobe
{
public string GetAdobeVersion()
{
try
{
RegistryKey adobe = Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Adobe");
if (adobe != null)
{
RegistryKey acroRead = adobe.OpenSubKey("Acrobat Reader");
if (acroRead != null)
{
string[] acroReadVersions = acroRead.GetSubKeyNames();
foreach (string versionNumber in acroReadVersions)
{
Console.WriteLine("Acrobat Reader version: " + versionNumber);
}
}
}
}
catch
{
}
return null;
}
}
}
I have small problem with simple code. This code is working properly on "x86" mode but not on "Any CPU" mode, maybe it is possible to run one class on "x86" and another class on "Any CPU" mode? Code:
namespace Software_Info_v1._0
{
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
public class Adobe
{
public string GetAdobeVersion()
{
try
{
RegistryKey adobe = Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Adobe");
if (adobe != null)
{
RegistryKey acroRead = adobe.OpenSubKey("Acrobat Reader");
if (acroRead != null)
{
string[] acroReadVersions = acroRead.GetSubKeyNames();
foreach (string versionNumber in acroReadVersions)
{
Console.WriteLine("Acrobat Reader version: " + versionNumber);
}
}
}
}
catch
{
}
return null;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为注册表重定向。
32 位和 64 位操作系统的注册表结构不同。
假设您在 64 位计算机上运行应用程序,针对 x86 目标进行编译会使您的程序使用 WOW64 模式(64 位上的 32 位进程)运行,并且您正在读取 Wow6432Node 下的密钥。请参阅 在 C# 中读取注册表时的奇怪行为
This is because of registry redirection.
The structure of the registry is different for 32-bit and 64-bit OS.
Assuming you are running your application on a 64-bit machine, compiling for x86 target makes your program run using WOW64 mode (32-bit process on 64-bit) and you're reading keys under the Wow6432Node. See Weird behaviour when reading registry in C#
当以 32 位运行时,注册表项会被重定向。当您以 64 位运行时,它不会被重定向,因此不会再点击 adobe 的注册表项被重定向的键。
因此,我将创建一个 Find32BitRegEntry(string path) 函数,该函数在 32 位上不执行任何操作,并在 x64 上添加重定向。
When running as 32bit, the registry key gets redirected. When you run as 64bit, it won't get redirected, and thus won't hit the key to which adobe's registry entry got redirected anymore.
So I'd create a
Find32BitRegEntry(string path)
function, that does nothing on 32bit, and adds the redirect on x64.注册表项可以位于 64 位计算机上的不同位置 - 请参阅 此。 (请注意,示例代码中的RegistryKey来自Microsoft.Win32?)
我认为您需要使用注册表重定向器,有一些关于它的讨论 此处。
The registry keys can be in a different place on 64 bit machines - see this. (Notice that RegistryKey in your sample code comes from Microsoft.Win32 ?)
I think you need to use a Registry Redirector, there's some talk about it over here.