Running a(ny) shell command using C#






4.55/5 (7 votes)
Running a(ny) shell command using C#
I was recently looking on the internet for a code piece that will run a shell-command just the way the WinKey+R 'Run' text box does.
The reason I wanted to do it, is that I wanted to write a "helper" for SlickRun that will
allow me to add 'MagicWords' using a tool that replicates the .NET's
System.String.Format()
method.
I kept looking for such a code piece, but none that I found offered the way to run a URL using the default browser.
So, after looking in all the shell's command's, I came across the command 'start'.
The 'start' command allowed me when calling it from C# to run a URL.
ProcessStartInfo info = new ProcessStartInfo { FileName = "cmd", UseShellExecute = true, Arguments = string.Format(@"/c start {0}", appArgs), };Now, that will allow you to run any URL/Shell-Command. P.S. If you want the same code piece for SlickRun, I used the following (somewhat ugly) code:
string appArgsFormat = args[0];
string appArgs;
try
{
appArgs = string.Format(appArgsFormat, args.Where((s, i) => i != 0).ToArray());
}
catch
{
// The way SlickRun gives the program all arguments using the $W$ keyword,
// is all those arguments seperated in '+' and not in ' '.
string[] splitArgs = args[1].Split('+');
if (args.Length == 2 && splitArgs.Length != 1)
{
Main(new[]{ appArgsFormat }.Concat(splitArgs).ToArray());
return;
}
MessageBox.Show(@"Application cannot execute. Probably not enough parameters to match format.");
return;
}
That way, I could create myself the following MagicWord:
Name: dexter Path: C:\ShellStartFormat.exe Arguments: http://www.sidereel.com/Dexter/season-{0}/episode-{1}/search $W$So, entering 'dexter 1 1' will direct me to 'http://www.sidereel.com/Dexter/season-1/episode-1/search[^]. Hope you can find this useful.