如何从注册表检查文件并使用C#获取文件路径 [英] How to check a file from registry and get the file path using C#
本文介绍了如何从注册表检查文件并使用C#获取文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我有C#应用程序,我想检查注册表中是否注册的工具。如果该工具已注册,我想获取路径并在命令提示符下执行该exe文件?
任何人都可以建议我,怎么做?
谢谢。
我的尝试:
Hello,
I have C# application, i want to check tool which is registered or not in the registry. if the tool is registered i want to get the path and execute that exe in command prompt?
can anyone suggest me, How to do this?
Thanks.
What I have tried:
using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\PenMount Windows Universal Driver"))
{
if (rk != null)
{
this.Logger.Error("PenMount exists");
}
else
{
this.Logger.Error("not exists");
}
}
但rk总是为空。
but rk is always getting null.
推荐答案
最可能的原因是您在64位操作系统上运行32位应用程序:
c# - OpenSubKey()为我在regedit.exe中可以看到的注册表项返回null - Stack Overflow [ ^ ]
默认情况下,你会在寻找HKLM \Software \Wow6432Node
,感谢注册表重定向器:
Registry Redirector(Windows) [ ^ ]
假设您使用的是.NET 4.0或更高版本,则需要指定注册表视图:
The most likely cause is that you're running a 32-bit application on a 64-bit OS:
c# - OpenSubKey() returns null for a registry key that I can see in regedit.exe - Stack Overflow[^]
By default, you'll be looking inHKLM\Software\Wow6432Node
, thanks to the registry redirector:
Registry Redirector (Windows)[^]
Assuming you're using .NET 4.0 or later, you'll need to specify the registry view:
using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
using (var key = hklm.OpenSubKey(@"SOFTWARE\PenMount Windows Universal Driver"))
{
// key now points to the 64-bit key
}
如果您使用的是早期版本,则需要进行一些P / Invoke调用:
如何从32位应用程序读取64位注册表,反之亦然押韵 [ ^ ]
这篇关于如何从注册表检查文件并使用C#获取文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文