1- using System ;
1+ using System ;
22using System . Collections . Generic ;
3+ using System . ComponentModel ;
34using System . Diagnostics ;
45using System . IO ;
56using System . Linq ;
7+ using System . Threading . Tasks ;
68using System . Windows ;
79using WindowsInput ;
810using WindowsInput . Native ;
@@ -84,7 +86,7 @@ public List<Result> Query(Query query)
8486 IcoPath = Image ,
8587 Action = c =>
8688 {
87- Execute ( m ) ;
89+ Execute ( Process . Start , PrepareProcessStartInfo ( m ) ) ;
8890 return true ;
8991 }
9092 } ) ) ;
@@ -117,7 +119,7 @@ private List<Result> GetHistoryCmds(string cmd, Result result)
117119 IcoPath = Image ,
118120 Action = c =>
119121 {
120- Execute ( m . Key ) ;
122+ Execute ( Process . Start , PrepareProcessStartInfo ( m . Key ) ) ;
121123 return true ;
122124 }
123125 } ;
@@ -136,7 +138,7 @@ private Result GetCurrentCmd(string cmd)
136138 IcoPath = Image ,
137139 Action = c =>
138140 {
139- Execute ( cmd ) ;
141+ Execute ( Process . Start , PrepareProcessStartInfo ( cmd ) ) ;
140142 return true ;
141143 }
142144 } ;
@@ -154,14 +156,14 @@ private List<Result> ResultsFromlHistory()
154156 IcoPath = Image ,
155157 Action = c =>
156158 {
157- Execute ( m . Key ) ;
159+ Execute ( Process . Start , PrepareProcessStartInfo ( m . Key ) ) ;
158160 return true ;
159161 }
160162 } ) . Take ( 5 ) ;
161163 return history . ToList ( ) ;
162164 }
163165
164- private void Execute ( string command , bool runAsAdministrator = false )
166+ private ProcessStartInfo PrepareProcessStartInfo ( string command , bool runAsAdministrator = false )
165167 {
166168 command = command . Trim ( ) ;
167169 command = Environment . ExpandEnvironmentVariables ( command ) ;
@@ -212,19 +214,33 @@ private void Execute(string command, bool runAsAdministrator = false)
212214 }
213215 else
214216 {
215- return ;
217+ throw new NotImplementedException ( ) ;
216218 }
217219
218220 info . UseShellExecute = true ;
219221
222+ _settings . AddCmdHistory ( command ) ;
223+
224+ return info ;
225+ }
226+
227+ private void Execute ( Func < ProcessStartInfo , Process > startProcess , ProcessStartInfo info )
228+ {
220229 try
221230 {
222- Process . Start ( info ) ;
223- _settings . AddCmdHistory ( command ) ;
231+ startProcess ( info ) ;
224232 }
225233 catch ( FileNotFoundException e )
226234 {
227- MessageBox . Show ( $ "Command not found: { e . Message } ") ;
235+ var name = "Plugin: Shell" ;
236+ var message = $ "Command not found: { e . Message } ";
237+ _context . API . ShowMsg ( name , message ) ;
238+ }
239+ catch ( Win32Exception e )
240+ {
241+ var name = "Plugin: Shell" ;
242+ var message = $ "Error running the command: { e . Message } ";
243+ _context . API . ShowMsg ( name , message ) ;
228244 }
229245 }
230246
@@ -306,19 +322,31 @@ public string GetTranslatedPluginDescription()
306322
307323 public List < Result > LoadContextMenus ( Result selectedResult )
308324 {
309- return new List < Result >
325+ var resultlist = new List < Result >
310326 {
311- new Result
312- {
313- Title = _context . API . GetTranslation ( "wox_plugin_cmd_run_as_administrator" ) ,
314- Action = c =>
315- {
316- Execute ( selectedResult . Title , true ) ;
317- return true ;
318- } ,
319- IcoPath = Image
320- }
321- } ;
327+ new Result
328+ {
329+ Title = _context . API . GetTranslation ( "wox_plugin_cmd_run_as_different_user" ) ,
330+ Action = c =>
331+ {
332+ Task . Run ( ( ) => Execute ( ShellCommand . RunAsDifferentUser , PrepareProcessStartInfo ( selectedResult . Title ) ) ) ;
333+ return true ;
334+ } ,
335+ IcoPath = "Images/user.png"
336+ } ,
337+ new Result
338+ {
339+ Title = _context . API . GetTranslation ( "wox_plugin_cmd_run_as_administrator" ) ,
340+ Action = c =>
341+ {
342+ Execute ( Process . Start , PrepareProcessStartInfo ( selectedResult . Title , true ) ) ;
343+ return true ;
344+ } ,
345+ IcoPath = Image
346+ }
347+ } ;
348+
349+ return resultlist ;
322350 }
323351 }
324352}
0 commit comments