tsucchi’s diary(元はてなダイアリー)

はてなダイアリー(d.hatena.ne.jp/tsucchi1022)から移行したものです

「アプリケーションの追加と削除」に記載されているプログラム名を取得する

ISO だか何だか知らんけど、会社では、インストールしたアプリケーションを届けないといけなくなった。めんどい。つーか、いちいち覚えてられないし、そもそも記憶ベースだと不正確だし。「Windows が覚えてるのなら、Windows にやらせればよくね?」と思って、コントロールパネルを開けたのだけど、「アプリケーションの追加と削除」の中身って、コピペできないのね orz。不便すぎ。

しゃーないので、自前で取得する物体を作ってみた。
正直なところ、これだと情報が過剰な上に、インストーラを使わないやつ(レジストリ書かないやつ)だと、取得できないっていう問題はある。でも記憶ベースよりはマシであろう。


参考にしたのは、この2つ。(1個はリンク切れだったので、archive.org から引っ張った) 「アプリケーションの追加と削除」エントリを取得するには?, - 参考:レジストリ、string()、For Each

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.IO;

namespace AppChecker
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            string uninstall_path = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
            RegistryKey uninstall = Registry.LocalMachine.OpenSubKey(uninstall_path, false);

            if ( uninstall != null )
            {
                StreamWriter result = new StreamWriter("result.txt");
                foreach ( string subKey in uninstall.GetSubKeyNames() )
                {
                    string appName = null;
                    RegistryKey appkey = Registry.LocalMachine.OpenSubKey(uninstall_path + "\\" + subKey, false);

                    if ( appkey.GetValue("DisplayName") != null )
                        appName = appkey.GetValue("DisplayName").ToString();
                    else
                        appName = subKey;

                    if ( !is_ignored_app(subKey, appName) )
                        result.WriteLine(appName);
                }
                result.Close();
            }
        }

        private static bool is_ignored_app(string subKey, string appName)
        {
            if ( subKey.StartsWith("KB") )//KB はとりあえず無視
                return true;
            if ( appName.StartsWith("{") && appName.EndsWith("}") ) //GUID 文字列(らしきもの)で、DisplayName が無い
                return true;
            return false;
        }
    }
}