查找具有相同前缀名称的C#中所有已安装软件的路径 [英] Finding path of all installed software in C# having the same prefix name

查看:146
本文介绍了查找具有相同前缀名称的C#中所有已安装软件的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#开发一个项目,我需要找到所有安装的软件的路径,以相同的前缀名开头。

例如,如果我的计算机中安装了10个软件,其中5个以前缀名称Microsoft开头,然后是其他一些后缀,那么我需要填充所有5个软件的名称以Microsoft开头及其安装路径。



我一次只识别1个软件但不能识别多个软件我找不到路径安装。

任何帮助都会有很大的动力。

提前致谢!!!



< pre lang =cs> public bool IsProgramInstalled( string programDisplayName)
{

Console.WriteLine( string .Format( <跨度class =code-string>检查安装状态:{0},programDisplayName));
foreach var item in Registry.LocalMachine.OpenSubKey( SOFTWARE \\ Microsoft \\\\ Windows \\ CurrentVersion \ \ Uninstall)。GetSubKeyNames())
{

object programName = Registry.LocalMachine.OpenSubKey( SOFTWARE \\ Microsoft \\\\ Windows \\ CurrentVersion \\Uninstall \\ + item)。GetValue( DisplayName);

Console.WriteLine(programName);

if string .Equals(programName,programDisplayName))
{
Console.WriteLine( 安装状态:INSTALLED);
return true ;
}
}
Console.WriteLine( 安装状态:NOT INSTALLED< /跨度>);
return false ;
}

解决方案

首先,我认为你正在复制和粘贴你在互联网上找到的代码并且不明白它的作用或工作原理。



您正在查找的(在注册表中)的InstallLocation不必填写,并且在大多数情况下不是'吨。这意味着您将无法找到许多安装包的安装位置。遗憾的是,这些信息被编码到许多安装程序中,并且没有办法可靠地从安装程序中获取该信息。


嗨会员,



我认为Dave Kreskowiak是正确的,你的方法无法可靠地捕获所有已安装的软件。

无论如何你想尝试这种方法:这里有一些代码可以执行搜索部分你问了。 (我不认为你会轻易获得相关的安装路径,我甚至没有尝试..,但是示例代码可以更改为仅提供其他内容而不仅仅是显示名称......)



 使用系统; 
使用 System.Collections.Generic;
使用 Microsoft.Win32;

命名空间 GetInstalledSoftware
{
class Program
{
静态 void Main( string [] args)
{
// 搜索所有已安装的软件使用Microsoft
string [] astrMatches = GetAllInstalledSoftware( 微软);

foreach string strDisplayName in astrMatches)
{
Console.WriteLine(strDisplayName);
}

Console.ReadKey();
}

静态 字符串 [] GetAllInstalledSoftware( string strPrefix)
{
const string strUNINSTALL_KEY = @ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall;
List< string> listMatches = new List< string>();

// 枚举在卸载键下找到的所有子键,每个子键代表已安装的软件
foreach string strSubKey in Registry.LocalMachine.OpenSubKey(strUNINSTALL_KEY).GetSubKeyNames())
{
// 尝试获取已安装软件的DisplayName
object objValue = Registry.LocalMachine.OpenSubKey(strUNINSTALL_KEY + @ \ + strSubKey).GetValue( DisplayName);
if (objValue!= null
{
string strDisplayName = objValue.ToString();

// 如果显示名称以所需前缀开头
if (strDisplayName.StartsWith(strPrefix))
{
// - >将其添加到结果列表
listMatches.Add(strDisplayName);
}
}
}
return listMatches.ToArray();
}
}
}





但要注意我不认为这是一个好解决方案:阅读注册表,依赖于DisplayName属性(并不总是设置),依赖于前缀(不在显示字符串内部搜索)等等。无论如何,我希望你不要使用那个代码任何现实世界的场景(看起来你甚至都有问题要理解你给出的示例代码 - 或者复制......所以我对初学者的一般建议:单独留下注册表:-)



如果您有任何其他问题 - 请随时询问。



亲切的问候



约翰内斯


I am working on a project in C# where i need to find the paths of all the software installed starting with a same prefix name.
For example , if i have 10 software's installed in my computer and 5 of them start with the prefix name say "Microsoft" and then followed by some other suffixes, then i need to populate all the 5 software's starting with the name "Microsoft" and their installation path.

I am able to identify only 1 software at a time but not multiple and i cannot locate the path of the installation.
Any help will be of great motivation.
Thanks in advance!!!

public  bool IsProgramInstalled(string programDisplayName)
        {

            Console.WriteLine(string.Format("Checking install status of: {0}", programDisplayName));
            foreach (var item in Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall").GetSubKeyNames())
            {

                object programName = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + item).GetValue("DisplayName");

                Console.WriteLine(programName);

                if (string.Equals(programName, programDisplayName))
                {
                    Console.WriteLine("Install status: INSTALLED");
                    return true;
                }
            }
            Console.WriteLine("Install status: NOT INSTALLED");
            return false;
        }

解决方案

First, I think you're copying and pasting code you found on the Internet and don't understand what it does or how it works.

The InstallLocation you're looking (in the registry) for does NOT have to be filled in, and in most cases isn't. This means that you're not going to be able to find where a package is installed for a lot of installations. That information, sadly, is coded into a lot of installers and there's no way to reliably get that information out of the installer.


Hi Member,

I think Dave Kreskowiak is correct that your approach won't catch all installed software reliably.
Anyway if you want to try this approach: here is some code that does the seaching part you asked for. (I don't think you will get the associated install paths easily, I even didn't try.., but the example code could be changed to give something else back than just the Display names...)

using System;
using System.Collections.Generic;
using Microsoft.Win32;

namespace GetInstalledSoftware
{
    class Program
    {
        static void Main(string[] args)
        {
            // Search for all installed software starting with "Microsoft"
            string[] astrMatches = GetAllInstalledSoftware("Microsoft");

            foreach (string strDisplayName in astrMatches)
            {
                Console.WriteLine(strDisplayName);
            }

            Console.ReadKey();
        }

        static string[] GetAllInstalledSoftware(string strPrefix)
        {
            const string strUNINSTALL_KEY = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
            List<string> listMatches = new List<string>();

            // Enumerate all sub keys found under the "Uninstall" key, each sub key represents a installed software
            foreach (string strSubKey in Registry.LocalMachine.OpenSubKey(strUNINSTALL_KEY).GetSubKeyNames())
            {
                // try to get the "DisplayName" for the installed software
                object objValue = Registry.LocalMachine.OpenSubKey(strUNINSTALL_KEY + @"\" + strSubKey).GetValue("DisplayName");
                if (objValue != null)
                {
                    string strDisplayName = objValue.ToString();

                    // If display name starts with the desired prefix
                    if (strDisplayName.StartsWith(strPrefix))
                    {
                        // -> add it to the result list
                        listMatches.Add(strDisplayName);
                    }
                }
            }
            return listMatches.ToArray();
        }
    }
}



But be aware that I don't think this is a "good" solution: reading registry, relying on the "DisplayName" property (which isn't always set), relying on the prefix (not seaching inside the Display string) etc. Anyway, I hope you don't use that code for any real world scenario (It seemed you even had problems to understand the example code you gave - or copied... So my general advice for beginners: leave the registry alone :-)

If you have any further questions - feel free to ask.

Kind regards

Johannes


这篇关于查找具有相同前缀名称的C#中所有已安装软件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆