OSDN Git Service

AppliStation-GUI,インストール先フォルダ」のところでの実行ファイルを直接実行したときに環境変数がきちんときくように
[applistation/AppliStation.git] / AppliStation / AppliStation.Util / ToolStripPetitLauncherMenuItem.cs
1 using System;\r
2 using System.Threading;\r
3 using System.IO;\r
4 using System.Drawing;\r
5 using System.Windows.Forms;\r
6 using System.Diagnostics;\r
7 using System.Text;\r
8 \r
9 namespace AppliStation.Util\r
10 {\r
11         public class ToolStripPetitLauncherMenuItem : ToolStripMenuItem\r
12         {\r
13                 private Thread thread;\r
14                 \r
15                 private string baseFolderPath;\r
16                 \r
17                 /// <summary>\r
18                 /// 親フォルダ\r
19                 /// </summary>\r
20                 public string BaseFolderPath {\r
21                         get { return baseFolderPath; }\r
22                         set {\r
23                                 baseFolderPath = value;\r
24                                 BuildItems();\r
25                         }\r
26                 }\r
27                 \r
28                 /// <summary>\r
29                 /// ドロップアイテムの(再)生成を行う\r
30                 /// </summary>\r
31                 public void BuildItems()\r
32                 {\r
33                         if (thread != null) {\r
34                                 if (! thread.Join(1000)) {\r
35                                         thread.Interrupt();\r
36                                 }\r
37                                 thread = null;\r
38                         }\r
39                         \r
40                         DropDownItems.Clear();\r
41                         if (Directory.Exists(baseFolderPath)) {\r
42                                 thread = new Thread(new ThreadStart(buildItems));\r
43                                 thread.Start();\r
44                         }\r
45                 }\r
46                 \r
47                 #region スレッド処理\r
48                 \r
49                 private delegate int ToolStripItemCollection_AddDelegate(ToolStripItem item);\r
50                 private void _addToItemsInv(ToolStripItem item)\r
51                 {\r
52                         if (Parent.InvokeRequired) {\r
53                                 Parent.Invoke(new ToolStripItemCollection_AddDelegate(DropDownItems.Add), item);\r
54                         } else {\r
55                                 DropDownItems.Add(item);\r
56                         }\r
57                 }\r
58                 private delegate void ToolStripItemCollection_InsertDelegate(int index, ToolStripItem item);\r
59                 private void _insertToItemsInv(int index, ToolStripItem item)\r
60                 {\r
61                         if (Parent.InvokeRequired) {\r
62                                 Parent.Invoke(new ToolStripItemCollection_InsertDelegate(DropDownItems.Insert), index, item);\r
63                         } else {\r
64                                 DropDownItems.Insert(index, item);\r
65                         }\r
66                 }\r
67                 \r
68                         \r
69                 private void buildItems()\r
70                 {\r
71                         try {\r
72                                 string folderPath = baseFolderPath;\r
73                                 ToolStripSeparator sep = new ToolStripSeparator();\r
74                                 bool cmdIsAdded = false;\r
75                                 \r
76                                 _addToItemsInv(CreateMenuItemForFolder(folderPath));\r
77                                 _addToItemsInv(sep);\r
78                                 \r
79                                 if (Directory.Exists(folderPath)) {\r
80                                         foreach (string exeFile in Directory.GetFiles(folderPath, "*.exe")) {\r
81                                                 if (baseFolderPath != folderPath) return; // 途中でなんか操作されているならば終了\r
82                                                 \r
83                                                 switch (NaGet.InteropServices.PEFileInfoUtils.GetPEFileType(exeFile)) {\r
84                                                         case NaGet.InteropServices.PEFileInfoUtils.PEFileType.WinGUI:\r
85                                                                 _addToItemsInv(CreateMenuItemForFile(exeFile));\r
86                                                                 break;\r
87                                                         case NaGet.InteropServices.PEFileInfoUtils.PEFileType.WinConsole:\r
88                                                                 if (! cmdIsAdded) {\r
89                                                                         _insertToItemsInv(0, CreateMenuItemForCmdAt(folderPath));\r
90                                                                         cmdIsAdded = true;\r
91                                                                 }\r
92                                                                 break;\r
93                                                 }\r
94                                         }\r
95                                 }\r
96                         } catch (ThreadInterruptedException) {}\r
97                 }\r
98                 \r
99                 #endregion\r
100                 \r
101                 /// <summary>\r
102                 /// 与えられたフォルダを開くメニューアイテムを生成する\r
103                 /// </summary>\r
104                 /// <param name="folderPath"></param>\r
105                 /// <returns></returns>\r
106                 public static ToolStripMenuItem CreateMenuItemForFolder(string folderPath)\r
107                 {\r
108                         ToolStripMenuItem item = CreateMenuItemForFile(folderPath);\r
109                         item.Text = "フォルダを開く(&O)";\r
110                         return item;\r
111                 }\r
112 \r
113                 /// <summary>\r
114                 /// 与えられたファイルのランチャーに相当するメニューアイテムを生成する\r
115                 /// </summary>\r
116                 /// <param name="filePath">ファイルパス</param>\r
117                 /// <returns>生成されたメニューアイテム</returns>\r
118                 public static ToolStripMenuItem CreateMenuItemForFile(string filePath)\r
119                 {\r
120                         ToolStripMenuItem item = new ToolStripMenuItem();\r
121                         string fileName = Path.GetFileName(filePath);\r
122                         \r
123                         item.Text = fileName;\r
124                         item.ShowShortcutKeys = false;\r
125                         item.Tag = filePath;\r
126                         item.Click += new System.EventHandler(menuItemForFileClicked);\r
127                         \r
128                         if (File.Exists(filePath)) {\r
129                                 item.Image = Icon.ExtractAssociatedIcon(filePath).ToBitmap();\r
130                                 \r
131                                 StringBuilder sb = new StringBuilder();\r
132                                 sb.AppendFormat("場所: {0}", Path.GetDirectoryName(filePath));\r
133                                 try {\r
134                                         FileVersionInfo vInfo = FileVersionInfo.GetVersionInfo(filePath);\r
135                                         \r
136                                         if (! string.IsNullOrEmpty(vInfo.FileDescription))\r
137                                                 sb.AppendFormat("\r\nファイルの説明: {0}", vInfo.FileDescription);\r
138                                         if (! string.IsNullOrEmpty(vInfo.CompanyName))\r
139                                                 sb.AppendFormat("\r\n会社: {0}", vInfo.CompanyName);\r
140                                         if (! string.IsNullOrEmpty(vInfo.FileVersion))\r
141                                                 sb.AppendFormat("\r\nファイルバージョン: {0}", vInfo.FileVersion);\r
142                                 } catch (Exception) {}\r
143                                 item.ToolTipText = sb.ToString();\r
144                         } else {\r
145                                 item.ToolTipText = filePath;\r
146                         }\r
147                         \r
148                         return item;\r
149                 }\r
150                 \r
151                 /// <summary>\r
152                 /// <see cref="CreateMenuItemForFile" />により使われるイベントハンドラ\r
153                 /// </summary>\r
154                 /// <param name="sender"></param>\r
155                 /// <param name="e"></param>\r
156                 private static void menuItemForFileClicked(object sender, EventArgs e)\r
157                 {\r
158                         ToolStripMenuItem item = (ToolStripMenuItem) sender;\r
159                         \r
160                         ProcessStartInfo procInfo = new ProcessStartInfo(item.Tag.ToString());\r
161                         procInfo.WorkingDirectory = Path.GetDirectoryName(procInfo.FileName);\r
162                         Process.Start(procInfo);\r
163                 }\r
164                 \r
165                 /// <summary>\r
166                 /// 与えられたフォルダのところでコマンドプロンプトを開く。\r
167                 /// </summary>\r
168                 /// <param name="folderPath">カレントフォルダ及びパスに追加されるフォルダ</param>\r
169                 /// <returns>生成されたメニューアイテム</returns>\r
170                 public static ToolStripMenuItem CreateMenuItemForCmdAt(string folderPath)\r
171                 {\r
172                         string cmdPath = Environment.GetEnvironmentVariable("comspec");\r
173                         if (string.IsNullOrEmpty(cmdPath) || (!File.Exists(cmdPath)))\r
174                                 throw new FileNotFoundException(null, Environment.ExpandEnvironmentVariables("%comspec%"));\r
175                         \r
176                         ToolStripMenuItem item = new ToolStripMenuItem();\r
177                         \r
178                         item.Text = string.Format("この場所で{0}を開く(&C)", (cmdPath.EndsWith(@"\command.com"))? "MS-DOSプロンプト" : "コマンドプロンプト");\r
179                         item.Tag = folderPath;\r
180                         item.Click += new System.EventHandler(menuItemForCmdAtClicked);\r
181                         try {\r
182                                 item.Image = Icon.ExtractAssociatedIcon(cmdPath).ToBitmap();\r
183                         } catch (Exception) {}\r
184                         \r
185                         return item;\r
186                 }\r
187                 \r
188                 /// <summary>\r
189                 /// <see cref="CreateMenuItemForCmdAt" />により使われるイベントハンドラ\r
190                 /// </summary>\r
191                 /// <param name="sender"></param>\r
192                 /// <param name="e"></param>\r
193                 private static void menuItemForCmdAtClicked(object sender, EventArgs e)\r
194                 {\r
195                         ToolStripMenuItem item = (ToolStripMenuItem) sender;\r
196                         string cmdPath = Environment.GetEnvironmentVariable("comspec");\r
197                         \r
198                         ProcessStartInfo procInfo = new ProcessStartInfo(cmdPath);\r
199                         procInfo.UseShellExecute = false;\r
200                         procInfo.WorkingDirectory = item.Tag.ToString();\r
201                         procInfo.EnvironmentVariables["PATH"] = Environment.GetEnvironmentVariable("path")\r
202                                 + Path.PathSeparator + item.Tag.ToString();\r
203                         Process.Start(procInfo);\r
204                 }\r
205         }\r
206 }\r