前回、カラーコードメーカーをつくりました。今回は保存したファイルをダブルクリックするとアプリが起動して開くことができるようにします。
これを参考にしました。
アプリケーションをファイルの拡張子に関連付ける – .NET Tips (VB.NET,C#…)
https://dobon.net/vb/dotnet/system/associatedapp.html
「HKEY_CLASSES_ROOT」は、「HKEY_LOCAL_MACHINE\Software\Classes」(すべてのユーザー共通の設定)と「HKEY_CURRENT_USER\Software\Classes」(ユーザーごとの設定)が合成されたものです。上記のように「HKEY_CLASSES_ROOT」にキーや値を作成した時、実際に作成される場所が「HKEY_LOCAL_MACHINE」と「HKEY_CURRENT_USER」のどちらになるかは、状況によって変わります。
もし「HKEY_LOCAL_MACHINE\Software\Classes」と「HKEY_CURRENT_USER\Software\Classes」のどちらに書き込むかはっきりさせたい場合は、「HKEY_CLASSES_ROOT」を使わない方が良いでしょう。例えば上記の例で「HKEY_CURRENT_USER\Software\Classes」に書き込みたいのであれば、「ClassesRoot」を「CurrentUser」とし、CreateSubKeyで作成するサブキー名の前に「Software\Classes\」を付けます。
ということなので、HKEY_CURRENT_USER\Software\Classes側に書き込むことにします。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | public partial class Form1 : Form {     void RelatingFile(string extension)     {         //実行するコマンドライン         string commandline = "\"" + Application.ExecutablePath + "\" \"%1\"";         //ファイルタイプ名         string fileType = Application.ProductName + ".0";         //説明(「ファイルの種類」として表示される)         string description = "MyApplication File";         //動詞         string verb = "open";         //動詞の説明(エクスプローラのコンテキストメニューに表示される。         // 省略すると、「開く(&O)」となる。)         string verbDescription = ProductName + "で開く(&O)";         //アイコンのパスとインデックス         string iconPath = Application.ExecutablePath;         int iconIndex = 0;         Microsoft.Win32.RegistryKey currentUserKey = Microsoft.Win32.Registry.CurrentUser;         //拡張子のキーを作成し、そのファイルタイプを登録         Microsoft.Win32.RegistryKey regkey = currentUserKey.CreateSubKey("Software\\Classes\\" + extension);         regkey.SetValue("", fileType);         regkey.Close();         //ファイルタイプのキーを作成し、その説明を登録         Microsoft.Win32.RegistryKey typekey = currentUserKey.CreateSubKey("Software\\Classes\\" + fileType);         typekey.SetValue("", description);         typekey.Close();         //動詞のキーを作成し、その説明を登録         Microsoft.Win32.RegistryKey verblkey = currentUserKey.CreateSubKey("Software\\Classes\\" + fileType + "\\shell\\" + verb);         verblkey.SetValue("", verbDescription);         verblkey.Close();         //コマンドのキーを作成し、実行するコマンドラインを登録         Microsoft.Win32.RegistryKey cmdkey = currentUserKey.CreateSubKey("Software\\Classes\\" + fileType + "\\shell\\" + verb + "\\command");         cmdkey.SetValue("", commandline);         cmdkey.Close();         //アイコンのキーを作成し、アイコンのパスと位置を登録         Microsoft.Win32.RegistryKey iconkey = currentUserKey.CreateSubKey("Software\\Classes\\" + fileType + "\\DefaultIcon");         iconkey.SetValue("", iconPath + "," + iconIndex.ToString());         iconkey.Close();     } } | 
そしてメニューをクリックしたら上記メソッドが実行されるようにしています。
| 1 2 3 4 5 6 7 | public partial class Form1 : Form {     private void MenuItemRelatingFile_Click(object sender, EventArgs e)     {         RelatingFile(".color");     } } | 
さてこれでファイルをダブルクリックしてもアプリが起動するだけでファイルは開かれません。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | public partial class Form1 : Form {     public Form1()     {         InitializeComponent();         // 初期設定 ファイルを関連付けとは関係ない         ShowColorValue();         pictureBox0.BackColor = GetColor();         SaveColor(pictureBox1, label1);         SaveColor(pictureBox2, label2);         SaveColor(pictureBox3, label3);         SaveColor(pictureBox4, label4);         SaveColor(pictureBox5, label5);         SaveColor(pictureBox6, label6);         SaveColor(pictureBox7, label7);         SaveColor(pictureBox8, label8);         // ファイルがダブルクリックされたとき         string[] files = System.Environment.GetCommandLineArgs();         var files1 = files.Skip(1);         foreach (var filePath in files1)         {             if (System.IO.File.Exists(filePath))             {                 try                 {                     OpenFile(filePath);                     break;                 }                 catch {                     MessageBox.Show(String.Format("{0}を開くことができません", filePath), "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);                 }             }         }     }     void OpenFile(string filePath)     {         var xml = new System.Xml.Serialization.XmlSerializer(typeof(List<int>));         var sr = new System.IO.StreamReader(filePath);         List<int> rgbs = (List<int>)xml.Deserialize(sr);         sr.Close();         SetColor(rgbs[0], pictureBox1, label1);         SetColor(rgbs[1], pictureBox2, label2);         SetColor(rgbs[2], pictureBox3, label3);         SetColor(rgbs[3], pictureBox4, label4);         SetColor(rgbs[4], pictureBox5, label5);         SetColor(rgbs[5], pictureBox6, label6);         SetColor(rgbs[6], pictureBox7, label7);         SetColor(rgbs[7], pictureBox8, label8);     } } | 
アプリケーション起動時のコマンドライン引数を取得するには、Environment.CommandLineプロパティを使います。ここではコマンドライン引数を半角スペースで分割した配列で取得するため、Environment.GetCommandLineArgsメソッドを使用しています。
CommandLineプロパティの内容はコマンドライン引数だけでなく、先頭に必ず実行ファイルのパスが付きます。そのため最初の1つはスキップしています。そして取得したファイルが存在するか確認してファイルを開いています。
実行ファイルのうえにファイルをドロップしたときもファイルを開くことができます。しかし.colorファイル以外のファイルもドロップできてしまいます。そこで例外処理を用いてファイルが開けない場合の対策をしています。
