如何在文本框中显示此代码中的信息

本文关键字:代码 信息 显示 文本 | 更新日期: 2023-09-27 17:49:44

我正在开发一个小工具,主要用于查找主机上安装的多个软件。我发现了一段代码,是一个更好的程序员用c#编写的,然而,我想知道两件事。1. 我用注册表标题替换什么来搜索已安装的软件?其次,我希望在文本框中显示找到的软件的名称。下面是代码。

 public static bool IsApplictionInstalled(string p_name)
    {
        string displayName;
        RegistryKey key;
        // search in: CurrentUser
        key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE'Microsoft'Windows'CurrentVersion'Uninstall");
        foreach (String keyName in key.GetSubKeyNames())
        {
            RegistryKey subkey = key.OpenSubKey(keyName);
            displayName = subkey.GetValue("DisplayName") as string;
            if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
            {
                return true;
            }
        }
        // search in: LocalMachine_32
        key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE'Microsoft'Windows'CurrentVersion'Uninstall");
        foreach (String keyName in key.GetSubKeyNames())
        {
            RegistryKey subkey = key.OpenSubKey(keyName);
            displayName = subkey.GetValue("DisplayName") as string;
            if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
            {
                return true;
            }
        }
        // search in: LocalMachine_64
        key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE'Wow6432Node'Microsoft'Windows'CurrentVersion'Uninstall");
        foreach (String keyName in key.GetSubKeyNames())
        {
            RegistryKey subkey = key.OpenSubKey(keyName);
            displayName = subkey.GetValue("DisplayName") as string;
            if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
            {
                return true;
            }
        }
        // NOT FOUND
        return false;
    }

如何在文本框中显示此代码中的信息

试试这个-注释是内联的。我假设代码检索键是正确的。还要记住,不同的系统有不同的注册表项,因此您需要创建逻辑来识别您在哪个系统上运行它,然后遍历可能的位置,您可能还需要创建逻辑,这将清除重复的程序名称。

// This is just to show that you need to create text box which needs to be set to multiline
var tb = new TextBox();
tb.Multiline = true;
// search in: LocalMachine_32
key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE'Microsoft'Windows'CurrentVersion'Uninstall");
foreach (String keyName in key.GetSubKeyNames())
{
    RegistryKey subkey = key.OpenSubKey(keyName);
    // here just add a line with a program name        
    string name = subkey.GetValue("DisplayName") as string;      
    if (!string.IsNullOrEmpty(name))
    {
        tb.Text += name;
        tb.Text += "'n'r";
    }
}