C#使用枚举方法来实现读取用户电脑中安装的软件

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
namespace project
{
    public class RegistryOperation
    {
        public static void Main(string[] args)
        {
            //定义顶级节点的路径
            RegistryKey ourkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\");
            //调用方法来枚举出该节点下的所有子项
            GetSubKeys(ourkey);
            //下面一句是让用户按下一个键后关闭程序
            Console.ReadKey(true);
        }
        /// <summary>
        /// 枚举出注册表某项下面的所有子项
        /// </summary>
        /// <param name="SubKey"></param>
        private static void GetSubKeys(RegistryKey SubKey)
        {
            foreach (string sub in SubKey.GetSubKeyNames())
            {
                //string subKey = string.Format(SubKey + @"\" + sub);
                //Console.Write(SubKey + @"\" + sub);
                RegistryKey local = Registry.Users;
                local = SubKey.OpenSubKey(sub, true);
                Console.WriteLine(local.GetValue("DisplayName","未知"));    //读取出安装的软件名称,未读出的显示未知
                GetSubKeys(local); //调用自身来查找剩余子项
            }
        }
    }
}

 

posted @ 2013-05-03 14:31  Micmacs  阅读(330)  评论(0编辑  收藏  举报