65.9K
CodeProject 正在变化。 阅读更多。
Home

安装在 Windows 机器上的软件列表

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.18/5 (12投票s)

2008年9月30日

CPOL

1分钟阅读

viewsIcon

62535

downloadIcon

1211

此程序获取在 Windows 运行机器上安装的软件列表

引言

我们很多人不知道安装了哪些软件,并且努力通过编程来检查它们。这是一种获取该列表的简单方法。当您想要为您的项目创建安装程序时,这非常有用,这有助于您检查客户端机器上您的项目的最低软件要求的正确版本。

背景

这其中没有复杂的代码。如果您对 Windows 注册表有基本的了解,那就足够了。但请务必在对注册表进行任何操作之前备份您的注册表,因为这是编程的一种良好且安全的方式。

Using the Code

我在 C# 中采用了一个简单的控制台应用程序,为访问正确的路径(即 SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall)的注册表创建了一个对象。

此注册表路径包含机器上安装的所有软件的列表。以下是在 C# 中实现它的代码。

在C#中

class Program
    {
        static void Main(string[] args)
        {
          // Do back up of ur registry before running it.
          //Registry path which has information of all the softwares installed on machine
            string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
            using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(uninstallKey))
            {
                foreach (string skName in rk.GetSubKeyNames())
                {
                    using (RegistryKey sk = rk.OpenSubKey(skName))
                    {
                        // we have many attributes other than these which are useful.
                        Console.WriteLine(sk.GetValue("DisplayName") + 
				"  " + sk.GetValue("DisplayVersion"));
                    }
                    
                }
            }
            Console.ReadLine(); // To make the o/p readable.
        }        
    }	

在 VB.NET 中

Imports System 
Imports System.Collections.Generic 
Imports System.Text 
Imports Microsoft.Win32 

Namespace SEList 
    Class Program 
        Private Shared Sub Main(args As String()) 
           ' Do back up of ur registry before running it. 
           'Registry path which has information of all the softwares installed on machine 
       Dim uninstallKey As String = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" 
            Using rk As RegistryKey = Registry.LocalMachine.OpenSubKey(uninstallKey) 
                For Each skName As String In rk.GetSubKeyNames() 
                    Using sk As RegistryKey = rk.OpenSubKey(skName) 
                        ' we have many attributes other than these which are useful. 
     Console.WriteLine(sk.GetValue("DisplayName") + " " + sk.GetValue("DisplayVersion")) 
                   
        End Using 
         Next 
        End Using 
        Console.ReadLine() ' To make the o/p readable. 
        End Sub 
    End Class 
End Namespace	 

output.jpg

关注点

这段代码片段对于从事为项目创建安装程序的开发人员来说非常方便,这使他们能够灵活地检查客户端机器上已安装软件的正确版本,以避免以后出现混淆。这是一种创建安装程序的良好方式。这对于在 Microsoft .NET 环境中开发的所有类型的应用程序都很有用。

历史

  • 2008 年 9 月 30 日:初始帖子

这是我在 CodeProject 上的第一篇文章。希望将来发布更多有用的代码片段。请留下您的建议和评论,因为这些都是激励我们并帮助我们提供更多相关和无错误的代码片段的内容。

© . All rights reserved.