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();
        }
    }
}
相关推荐
WSSWWWSSW1 小时前
Numpy科学计算与数据分析:Numpy文件操作入门之数组数据的读取和保存
开发语言·python·数据挖掘·数据分析·numpy
芥子须弥Office2 小时前
从C++0基础到C++入门 (第二十五节:指针【所占内存空间】)
c语言·开发语言·c++·笔记
Q741_1473 小时前
如何判断一个数是 2 的幂 / 3 的幂 / 4 的幂 / n 的幂 位运算 总结和思考 每日一题 C++的题解与思路
开发语言·c++·算法·leetcode·位运算·总结思考
钢铁男儿3 小时前
深入解析C#并行编程:从并行循环到异步编程模式
开发语言·c#
小杜的生信筆記4 小时前
基于R语言,“上百种机器学习模型”学习教程 | Mime包
开发语言·学习·机器学习·r语言·sci
源代码•宸5 小时前
C++高频知识点(十八)
开发语言·c++·经验分享·多线程·互斥锁·三次握手·字节对齐
mit6.8245 小时前
修复C++14兼容性问题& 逻辑检查
开发语言·c++
沐知全栈开发5 小时前
MongoDB 高级索引
开发语言
测试界清流6 小时前
Postman接口测试入门
开发语言·lua