refactor to use ShellCommand

This commit is contained in:
Jeremy 2021-11-17 21:00:36 +11:00
parent d41dd780ce
commit ae7de8db09
5 changed files with 34 additions and 14 deletions

View file

@ -33,6 +33,8 @@ namespace Flow.Launcher.Plugin
/// Run a shell command or external program /// Run a shell command or external program
/// </summary> /// </summary>
/// <param name="cmd">The command or program to run</param> /// <param name="cmd">The command or program to run</param>
/// <exception cref="FileNotFoundException">Thrown when unable to find the file specified in the command </exception>
/// <exception cref="Win32Exception">Thrown when error occurs during the execution of the command </exception>
void ShellRun(string cmd); void ShellRun(string cmd);
/// <summary> /// <summary>

View file

@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
@ -60,17 +60,39 @@ namespace Flow.Launcher.Plugin.SharedCommands
return sb.ToString(); return sb.ToString();
} }
public static ProcessStartInfo SetProcessStartInfo(this string fileName, string workingDirectory = "", string arguments = "", string verb = "") public static ProcessStartInfo SetProcessStartInfo(this string fileName, string workingDirectory = "", string arguments = "", string verb = "", bool createNoWindow = false)
{ {
var info = new ProcessStartInfo var info = new ProcessStartInfo
{ {
FileName = fileName, FileName = fileName,
WorkingDirectory = workingDirectory, WorkingDirectory = workingDirectory,
Arguments = arguments, Arguments = arguments,
Verb = verb Verb = verb,
CreateNoWindow = createNoWindow
}; };
return info; return info;
} }
/// <summary>
/// Runs a windows command using the provided ProcessStartInfo
/// </summary>
/// <exception cref="FileNotFoundException">Thrown when unable to find the file specified in the command </exception>
/// <exception cref="Win32Exception">Thrown when error occurs during the execution of the command </exception>
public static void Execute(ProcessStartInfo info)
{
Execute(Process.Start, info);
}
/// <summary>
/// Runs a windows command using the provided ProcessStartInfo using a custom execute command function
/// </summary>
/// <param name="Func startProcess">allows you to pass in a custom command execution function</param>
/// <exception cref="FileNotFoundException">Thrown when unable to find the file specified in the command </exception>
/// <exception cref="Win32Exception">Thrown when error occurs during the execution of the command </exception>
public static void Execute(Func<ProcessStartInfo, Process> startProcess, ProcessStartInfo info)
{
startProcess(info);
}
} }
} }

View file

@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
@ -14,6 +14,7 @@ using Flow.Launcher.Infrastructure.Image;
using Flow.Launcher.Plugin; using Flow.Launcher.Plugin;
using Flow.Launcher.ViewModel; using Flow.Launcher.ViewModel;
using Flow.Launcher.Plugin.SharedModels; using Flow.Launcher.Plugin.SharedModels;
using Flow.Launcher.Plugin.SharedCommands;
using System.Threading; using System.Threading;
using System.IO; using System.IO;
using Flow.Launcher.Infrastructure.Http; using Flow.Launcher.Infrastructure.Http;
@ -102,15 +103,10 @@ namespace Flow.Launcher
SettingWindow sw = SingletonWindowOpener.Open<SettingWindow>(this, _settingsVM); SettingWindow sw = SingletonWindowOpener.Open<SettingWindow>(this, _settingsVM);
}); });
} }
public void ShellRun(string cmd) public void ShellRun(string cmd)
{ {
System.Diagnostics.Process process = new(); var startInfo = ShellCommand.SetProcessStartInfo("cmd.exe", arguments: $"/C {cmd}", createNoWindow: true);
var startInfo = process.StartInfo; ShellCommand.Execute(startInfo);
startInfo.FileName = "cmd.exe";
startInfo.Arguments = $"/C {cmd}";
startInfo.CreateNoWindow = true;
process.Start();
} }
public void StartLoadingBar() => _mainVM.ProgressBarVisibility = Visibility.Visible; public void StartLoadingBar() => _mainVM.ProgressBarVisibility = Visibility.Visible;

View file

@ -251,7 +251,7 @@ namespace Flow.Launcher.Plugin.Shell
{ {
try try
{ {
startProcess(info); ShellCommand.Execute(startProcess, info);
} }
catch (FileNotFoundException e) catch (FileNotFoundException e)
{ {

View file

@ -190,8 +190,8 @@ namespace Flow.Launcher.Plugin.Sys
var info = ShellCommand.SetProcessStartInfo("shutdown", arguments:"/h"); var info = ShellCommand.SetProcessStartInfo("shutdown", arguments:"/h");
info.WindowStyle = ProcessWindowStyle.Hidden; info.WindowStyle = ProcessWindowStyle.Hidden;
info.UseShellExecute = true; info.UseShellExecute = true;
Process.Start(info); ShellCommand.Execute(info);
return true; return true;
} }