Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run commandline from c# with parameters?

Tags:

c#

.net

process

It's possible to run commandline in c# by using something like this :

process = new Process();
process.StartInfo.FileName = command;
process.Start();

The problem is if the command string contains parameters, for example:

C:\My Dir\MyFile.exe MyParam1 MyParam2

This will not work and I don't see how to extract the parameters from this string and set it on the process.Arguments property? The path and filename could be something else, the file does not have to end with exe.

How can I solve this?

like image 576
Banshee Avatar asked May 11 '11 12:05

Banshee


People also ask

Can C run cmd?

We usually use a compiler with a graphical user interface, to compile our C program. This can also be done by using cmd. The command prompt has a set of steps we need to perform in order to execute our program without using a GUI compiler.

How do I run a Command Prompt?

Type "start [filename.exe]" into Command Prompt, replacing "filename" with the name of your selected file. Replace "[filename.exe]" with your program's name. This allows you to run your program from the file path.

What is C in Windows command line?

/C Carries out the command specified by the string and then terminates. You can get all the cmd command line switches by typing cmd /? .


1 Answers

If I understand correctly I would use:

string command = @"C:\My Dir\MyFile.exe";
string args = "MyParam1 MyParam2";

Process process = new Process(); 
process.StartInfo.FileName = command; 
process.StartInfo.Arguments = args;
process.Start(); 

If you have a complete string that you need to parse I would use the other methods put forward by the others here. If you want to add parameters to the process, use the above.

like image 121
anothershrubery Avatar answered Sep 19 '22 04:09

anothershrubery