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

Check installed app path in reg

我正在阅读此 post 此处:Check if application is installed in registry

我真的很喜欢 Mroczny Arturek 的示例,但是我似乎无法找出将此代码也实施到 return 应用程序安装路径的最佳方法。

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,如果已安装该程序,它将 return 所需的 exe 路径。