gpt4 book ai didi

检查reg中安装的应用程序路径

转载 作者:行者123 更新时间:2023-11-30 16:31:04 26 4
gpt4 key购买 nike

我在这里读这篇文章:Check if application is installed in registry

我真的很喜欢 Mroczny Arturek 的示例,但是我似乎无法找出实现此代码以返回应用程序安装路径的最佳方法。

public enum ProgramVersion
{
x86,
x64
}

private static IEnumerable<string> GetRegisterSubkeys(RegistryKey registryKey)
{
return registryKey.GetSubKeyNames()
.Select(registryKey.OpenSubKey)
.Select(subkey => subkey.GetValue("DisplayName") as string)
}

private static bool CheckNode(RegistryKey registryKey, string applicationName, ProgramVersion? programVersion)
{
return GetRegisterSubkeys(registryKey).Any(displayName => displayName != null
&& displayName.Contains(applicationName)
&& displayName.Contains(programVersion.ToString()));
}

private static bool CheckApplication(string registryKey, string applicationName, ProgramVersion? programVersion)
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey);

if (key != null)
{
if (CheckNode(key, applicationName, programVersion))
return true;

key.Close();
}

return false;
}

public static bool IsSoftwareInstalled(string applicationName, ProgramVersion? programVersion)
{
string[] registryKey = new[] {
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
};

return registryKey.Any(key => CheckApplication(key, applicationName, programVersion));
}

您建议如何做?

.Select(subkey => subkey.GetValue("InstallLocation") as string);

最佳答案

public enum ProgramVersion
{
x86,
x64
}

private static IEnumerable<string> GetRegisterSubkeys(RegistryKey registryKey)
{
return registryKey.GetSubKeyNames()
.Select(registryKey.OpenSubKey)
.Select(subkey => subkey.GetValue("DisplayName") as string);
}

private static bool CheckNode(RegistryKey registryKey, string applicationName, ProgramVersion? programVersion)
{
return GetRegisterSubkeys(registryKey).Any(displayName => displayName != null
&& displayName.Contains(applicationName)
&& displayName.Contains(programVersion.ToString()));
}

private static bool CheckApplication(string registryKey, string applicationName, ProgramVersion? programVersion)
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey);

if (key != null)
{
if (CheckNode(key, applicationName, programVersion))
return true;

key.Close();
}

return false;
}

public static bool IsSoftwareInstalled(string applicationName, ProgramVersion? programVersion)
{
string[] registryKey = new[] {
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
};

return registryKey.Any(key => CheckApplication(key, applicationName, programVersion));
}



private void button1_Click(object sender, EventArgs e)
{
if (IsSoftwareInstalled("Type Program EXE Name HERE Without Using .exe", null))
{
var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall") ??
Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");


if (key == null)
{
return;
}
else
{
foreach (string subkey_name in key.GetSubKeyNames())
{
//MessageBox.Show(subkey_name); //Commented this out, i use this to iterate through and show me the install folder names.
using (RegistryKey subkey = key.OpenSubKey(subkey_name))
{
if (!object.ReferenceEquals(subkey.GetValue("DisplayName"), null))
{
string[] str = subkey.GetValueNames();
string SoftNames = Convert.ToString(subkey.GetValue("DisplayName"));
if (SoftNames == "Type HERE the DisPlayName")
{
string InstallLoc = Convert.ToString(subkey.GetValue("InstallLocation"));
//Strip the "double quotes"
string stripped = InstallLoc.Replace("\"", "");


MessageBox.Show(stripped + @"\Program.exe");
}
}
}
}
}
}
else
{
MessageBox.Show("Program not installed");
}
}

这会检查注册表中是否有 64/32 已安装的程序,如果该程序已安装,它将返回所需的 exe 路径。

关于检查reg中安装的应用程序路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50816166/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com