Running an application using cmd with command line in C#

661 Views Asked by At

I am working on a project that needs the cmd will run the application.

with an automatic filling of the textbox of application.

Currently, I have seen this code, but this does not work.

It throws this exception - StandardIn has not been redirected

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\Windows\System32\cmd.exe";                  
process.StartInfo = startInfo;
process.StandardInput.WriteLine(@"C:\Program Files\WinMerge\WinMergeU.exe" + txtJOB.Text + txtKJOB.Text + "- minimize - noninteractive - noprefs - cfg Settings / DirViewExpandSubdirs = 1 - cfg ReportFiles / ReportType = 2 - cfg ReportFiles / IncludeFileCmpReport = 1 - r - u -or" + txtResultPath.Text);
process.Start();

If i use cmd and run this line

"C:\Program Files\WinMerge\WinMergeU.exe" + txtJOB.Text + txtKJOB.Text + "- minimize - noninteractive - noprefs - cfg Settings / DirViewExpandSubdirs = 1 - cfg ReportFiles / ReportType = 2 - cfg ReportFiles / IncludeFileCmpReport = 1 - r - u -or" + txtResultPath.Text

this does work. but how will i Implement this command line in c#?

can someone help me with this?

Thanks in Advance.

2

There are 2 best solutions below

1
On BEST ANSWER

Your exception is thrown because you writing a command to standart input of process (process.StandardInput.WriteLine()) before you actually start process (process.Start()).

If you just need to start a WinMergeU - you don't need to call cmd.exe at all, this can be done like this:

var fileName = @"C:\Program Files\WinMerge\WinMergeU.exe";
var arguments = $"{txtJOB.Text} {txtKJOB.Text} -minimize -noninteractive -noprefs " +
     "-cfg Settings/DirViewExpandSubdirs=1 -cfg ReportFiles/ReportType=2 " +
    $"-cfg ReportFiles/IncludeFileCmpReport=1 -r -u -or {txtResultPath.Text}";

Process.Start(fileName, arguments);
2
On

Use the Arguments property on ProcessStartInfo

Process process = new Process();
ProcessStartInfo startInfo = new 
ProcessStartInfo(@"C:\Windows\System32\cmd.exe");
startInfo.Arguments = @"C:\Program Files\WinMerge\WinMergeU.exe" + txtJOB.Text + txtKJOB.Text + "- minimize - noninteractive - noprefs - cfg Settings / DirViewExpandSubdirs = 1 - cfg ReportFiles / ReportType = 2 - cfg ReportFiles / IncludeFileCmpReport = 1 - r - u -or" + txtResultPath.Text;
process.StartInfo = startInfo;
process.Start();

```