C# 查询电脑已安装所有软件并打印txt保存到桌面

cs 复制代码
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var installedApps = new List<string>();

                string[] registryPaths = new string[]
                {
                @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
                @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
                };

                RegistryKey[] rootKeys = new RegistryKey[]
                {
                Registry.LocalMachine,
                Registry.CurrentUser
                };

                foreach (var root in rootKeys)
                {
                    foreach (var path in registryPaths)
                    {
                        using (RegistryKey key = root.OpenSubKey(path))
                        {
                            if (key == null) continue;

                            foreach (var subkeyName in key.GetSubKeyNames())
                            {
                                using (RegistryKey subkey = key.OpenSubKey(subkeyName))
                                {
                                    if (subkey == null) continue;

                                    string displayName = subkey.GetValue("DisplayName") as string;
                                    string displayVersion = subkey.GetValue("DisplayVersion") as string;
                                    string publisher = subkey.GetValue("Publisher") as string;
                                    string installLocation = subkey.GetValue("InstallLocation") as string;
                                    string installSource = subkey.GetValue("InstallSource") as string;
                                    string uninstallString = subkey.GetValue("UninstallString") as string;

                                    if (!string.IsNullOrEmpty(displayName))
                                    {
                                        installedApps.Add($"{displayName} 版本: {displayVersion ?? "未知"} 发布者: {publisher ?? "未知"}");

                                        //优先用 InstallLocation,没有再用 InstallSource,再没有用 UninstallString
                                        string pathInfo = !string.IsNullOrEmpty(installLocation) ? installLocation
                                                         : !string.IsNullOrEmpty(installSource) ? installSource
                                                         : (!string.IsNullOrEmpty(uninstallString) ? uninstallString : "路径未知");

                                        installedApps.Add($"安装路径: {pathInfo}");
                                        installedApps.Add(""); 
                                    }
                                }
                            }
                        }
                    }
                }

                string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
                string outputFile = Path.Combine(desktopPath, "InstalledAppsList.txt");

                File.WriteAllLines(outputFile, installedApps);

                Console.WriteLine($"扫描完成,已保存到:{outputFile}");
            }
            catch (Exception ex)
            {
                Console.WriteLine("发生异常:" + ex.Message);
            }

            Console.WriteLine("按任意键退出...");
            Console.ReadKey();
        }
    }
}
相关推荐
⑩-8 分钟前
Blocked与Wati的区别
java·开发语言
csdn_aspnet15 分钟前
C# 电子签名及文档存储
javascript·c#
IManiy17 分钟前
Java表达式引擎技术选型分析(SpEL、QLExpress)
java·开发语言
历程里程碑25 分钟前
C++ 17异常处理:高效捕获与精准修复
java·c语言·开发语言·jvm·c++
雨雨雨雨雨别下啦27 分钟前
ssm复习总结
java·开发语言
拾贰_C1 小时前
【python | pytorch | 】.报错怎么找到问题所在?
开发语言·pytorch·python
JasmineWr1 小时前
Java SPI和OSGi
java·开发语言
Lisonseekpan1 小时前
@Autowired 与 @Resource区别解析
java·开发语言·后端
你的冰西瓜1 小时前
C++中的vector容器详解
开发语言·c++·stl
刻BITTER1 小时前
C++ 获取任意整数类型的最大、最小值和长度
开发语言·c++