如何从此代码在文本框中显示信息?
how can I display the information in a textbox from this code?
我正在开发一个小工具,它实际上是在主机上查找多个已安装的软件。我发现了一段代码,一个更好的程序员用 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";
}
}
我正在开发一个小工具,它实际上是在主机上查找多个已安装的软件。我发现了一段代码,一个更好的程序员用 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";
}
}