wpf 从注册表中检索文件安装路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12008331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieving file installation path from registry
提问by
I am creating a WPF utility which needs to access the registry of the local machine, to then find out the installation path of the program.
我正在创建一个 WPF 实用程序,它需要访问本地机器的注册表,然后找出程序的安装路径。
I've navigated to the key via Regedit and it gives a Name, Type and Data, within the Data it shows the installation path, I would like to extract the installation path.
我已经通过 Regedit 导航到密钥,它提供了名称、类型和数据,在数据中它显示了安装路径,我想提取安装路径。
I know I need to navigate to this key within the registry:
我知道我需要导航到注册表中的这个键:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\
then I need to access a folder within this key with the information regarding the installation path.
然后我需要访问此密钥中的文件夹,其中包含有关安装路径的信息。
-
——
回答by
I solved my problem, to anyone who wants a solution in the future if your still stuck after this please message me, I found it was hard to find the resources.
我解决了我的问题,如果您在此之后仍然遇到问题,请给我以后想要解决方案的任何人,我发现很难找到资源。
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\App Paths\myexe.exe");
string regFilePath = null;
object objRegisteredValue = key.GetValue("");
registeredFilePath = value.ToString();
回答by outcoldman
To read registry keys you should use Microsot.Windows.RegistryKey class, class Registrycan open for you the RegistryKey.
要读取注册表项,您应该使用Microsot.Windows.RegistryKey 类,Registry类可以为您打开 RegistryKey。
回答by Derek
This question was very helpful to me. I came up with a helper class, wanting to play with the new Tuples.
这个问题对我很有帮助。我想出了一个助手类,想玩新的元组。
Example usage:
用法示例:
public string SkypeExePath => InstalledApplicationPaths.GetInstalledApplicationPath( "lync.exe" );
The class:
班上:
public static class InstalledApplicationPaths
{
public static string GetInstalledApplicationPath( string shortName )
{
var path = GetInstalledApplicationPaths().SingleOrDefault( x => x?.ExectuableName.ToLower() == shortName.ToLower() )?.Path;
return path;
}
public static IEnumerable<(string ExectuableName, string Path)?> GetInstalledApplicationPaths()
{
using ( RegistryKey key = Registry.LocalMachine.OpenSubKey( @"Software\Microsoft\Windows\CurrentVersion\App Paths" ) )
{
foreach ( var subkeyName in key.GetSubKeyNames() )
{
using ( RegistryKey subkey = key.OpenSubKey( subkeyName ) )
{
yield return (subkeyName, subkey.GetValue( "" )?.ToString());
}
}
}
}
}

