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();
        }
    }
}
相关推荐
meilindehuzi_a4 小时前
从零理解并实现 RAG:用 LangChain.js 构建语义检索问答系统
开发语言·javascript·langchain
风流 少年4 小时前
Julia
开发语言·julia
江畔柳前堤4 小时前
GO01-Go 语言与主流编程语言深度对比
开发语言·人工智能·后端·微服务·云原生·golang·go
:-)5 小时前
算法-归并排序
java·开发语言·数据结构·算法·排序算法
GIS阵地6 小时前
QgsRasterDataProvider 完整详解(QGIS 3.40.13 C++)
开发语言·c++·qt·开源软件·qgis
yaoxin5211237 小时前
462. Java 反射 - 获取声明类与封闭类
java·开发语言·python
阿里嘎多学长7 小时前
2026-07-14 GitHub 热点项目精选
开发语言·程序员·github·代码托管
charlie1145141918 小时前
Cinux: 为大内核铺路
开发语言·c++·操作系统·现代c++
2501_914245938 小时前
C语言设计模式详解:从理论到实践的完整指南
c语言·开发语言·设计模式
Hazenix9 小时前
Go 指南:一篇文章速通 Golang
开发语言·后端·golang