【问题标题】:how can I display the information in a textbox from this code?如何在此代码的文本框中显示信息?
【发布时间】:2015-05-19 21:45:01
【问题描述】:

我正在开发一个小工具,该工具实质上是在主机上查找多个已安装的软件。我发现了一段代码,它是一个更好的程序员用 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;
    }

【问题讨论】:

  • +1 因为我正在反对匿名不明原因的反对选民。对我来说,这似乎是一个完全合理的问题。
  • @Tim Long,哈哈,我很欣赏那个哥们

标签: c# textbox return-value boolean-logic registrykey


【解决方案1】:

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

// 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";
    }
}

【讨论】:

  • 是的,我已经构建了检索有关操作系统、版本和位的系统信息的逻辑,因此在这方面我已做好准备。我假设我从卸载列表中获取程序的名称,并将其放在显示为“DisplayName”的字符串中;
  • 是的,如果你有名字 - 只需将它添加到文本框的新行中
猜你喜欢
  • 1970-01-01
  • 2011-12-22
  • 1970-01-01
  • 1970-01-01
  • 2012-10-11
  • 2014-08-05
  • 2021-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多