按照建议here ,我需要遍历
中的条目HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\
找出我的应用程序的安装路径。如何迭代以便我可以找出 给定 DisplayName 的 InstallLocation 值。如何在 C# 中高效地执行此操作。
最佳答案
下面是实现你的目标的代码:
using Microsoft.Win32;
class Program
{
static void Main(string[] args)
{
RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
foreach (var v in key.GetSubKeyNames())
{
Console.WriteLine(v);
RegistryKey productKey = key.OpenSubKey(v);
if (productKey != null)
{
foreach (var value in productKey.GetValueNames())
{
Console.WriteLine("\tValue:" + value);
// Check for the publisher to ensure it's our product
string keyValue = Convert.ToString(productKey.GetValue("Publisher"));
if (!keyValue.Equals("MyPublisherCompanyName", StringComparison.OrdinalIgnoreCase))
continue;
string productName = Convert.ToString(productKey.GetValue("DisplayName"));
if (!productName.Equals("MyProductName", StringComparison.OrdinalIgnoreCase))
return;
string uninstallPath = Convert.ToString(productKey.GetValue("InstallSource"));
// Do something with this valuable information
}
}
}
Console.ReadLine();
}
}
编辑:参见this method for a more comprehensive way to find an Applications Install Path ,它演示了如何按照注释中的建议处理 using
。
https://stackoverflow.com/a/26686738/495455
关于c# - 遍历注册表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1458653/