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();
        }
    }
}
相关推荐
SOC罗三炮11 小时前
OpenHuman 源码深度解构:一个 Rust 驱动的本地优先 AI 个人助手
开发语言·人工智能·rust
心怀梦想的咸鱼11 小时前
OpenCode 接入 API 报错 read ECONNRESET:基于环境变量的证书校验绕过方案
开发语言·php
程序大视界12 小时前
【Python系列课程】Python入门教程
开发语言·人工智能·python
morning_judger12 小时前
Agent系列(二)-记忆系统的设计
开发语言·python·机器学习
方也_arkling12 小时前
【Java-Day02】语法篇:变量/数据类型/标识符/运算符/类型转换
java·开发语言
RSTJ_162512 小时前
PYTHON+AI LLM DAY SIXTY-ONE
开发语言·python
zfoo-framework12 小时前
理解kotlin limitedParallelism(1)与Actor模型
android·开发语言·kotlin
.千余12 小时前
【C++】C++类与对象3:const成员函数与取地址运算符重载,权限管理的艺术
开发语言·c++
影寂ldy12 小时前
C# 类和对象
开发语言·c#
丷丩12 小时前
MapLibre GL JS第25课:添加栅格瓦片源
开发语言·javascript·gis·mapbox·maplibre gl js