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();
        }
    }
}
相关推荐
JS.Huang7 分钟前
【JavaScript】原生函数
开发语言·javascript·ecmascript
CoderCodingNo1 小时前
【GESP】C++五级考试大纲知识点梳理, (5) 算法复杂度估算(多项式、对数)
开发语言·c++·算法
星河队长1 小时前
VS创建C++动态库和C#访问过程
java·c++·c#
ftpeak2 小时前
JavaScript性能优化实战
开发语言·javascript·性能优化
William_cl2 小时前
【C# MVC 前置】异步编程 async/await:从 “卡界面” 到 “秒响应” 的 Action 优化指南(附微软官方避坑清单)
microsoft·c#·mvc
一个很帅的帅哥2 小时前
JavaScript事件循环
开发语言·前端·javascript
驰羽2 小时前
[GO]gin框架:ShouldBindJSON与其他常见绑定方法
开发语言·golang·gin
程序员大雄学编程2 小时前
「用Python来学微积分」5. 曲线的极坐标方程
开发语言·python·微积分
yong99902 小时前
C#驱动斑马打印机实现包装自动打印
java·数据库·c#
Jose_lz3 小时前
C#开发学习杂笔(更新中)
开发语言·学习·c#