Click here to Skip to main content
15,886,787 members
Articles / Programming Languages / Shell
Tip/Trick

Running a(ny) shell command using C#

Rate me:
Please Sign up or sign in to vote.
4.55/5 (7 votes)
24 Jan 2011CPOL 30.1K   8   9
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:

C#
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 4 Thanks for sharing Pin
Patrick Kalkman12-Feb-11 10:32
Patrick Kalkman12-Feb-11 10:32 
GeneralReason for my vote of 5 Nice tip! Pin
Sandeep Mewara12-Feb-11 9:12
mveSandeep Mewara12-Feb-11 9:12 
GeneralReason for my vote of 5 Nice tip! Pin
Manfred Rudolf Bihy12-Feb-11 7:18
professionalManfred Rudolf Bihy12-Feb-11 7:18 
GeneralReason for my vote of 4 Keep going Erez...good tip.. Pin
Pravin Patil, Mumbai24-Jan-11 3:27
Pravin Patil, Mumbai24-Jan-11 3:27 
QuestionCan the following be achieved ??? Pin
All Time Programming14-Feb-11 18:41
All Time Programming14-Feb-11 18:41 
AnswerRe: Can the following be achieved ??? Pin
Erez Zinman15-Feb-11 13:18
Erez Zinman15-Feb-11 13:18 
QuestionRe: Can the following be achieved ??? [modified] Pin
All Time Programming16-Feb-11 4:59
All Time Programming16-Feb-11 4:59 
AnswerRe: Can the following be achieved ??? Pin
Erez Zinman17-Feb-11 7:39
Erez Zinman17-Feb-11 7:39 
GeneralNice tip Pin
Henry Minute22-Jan-11 3:54
Henry Minute22-Jan-11 3:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.