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?
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.
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.
/C Carries out the command specified by the string and then terminates. You can get all the cmd command line switches by typing cmd /? .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With