2

Although there are some explanations regarding my problem, I can't solve the following cmd command issue. My goal is to start an .exe with some parameters through C#, which has to manipulate more than one file in a row. Even if I wait until the process has finished, it does not end. Without waiting for the end (Process.WaitForExit()), it looks like the different commands are terminating each other without execution. How can I achieve, with the following method, execution of each .exe file I want to?

ProcessStartInfo startInfo = new ProcessStartInfo();

startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = false;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = @"\c Path_of_the_exe " + Path_of_the_file;

using (Process exeProcess = Process.Start(startInfo))
{
   exeProcess.WaitForExit();
}
2
  • 1
    Shouldn't it read /c instead of \c? Commented Aug 19, 2019 at 12:13
  • 5
    Why bother using CMD to launch the app when you could just call the executable directly? Commented Aug 19, 2019 at 12:18

1 Answer 1

3

I can't think of any reason why you would need to use cmd.exe to launch an exe. You can just launch your exe directly:

ProcessStartInfo startInfo = new ProcessStartInfo();

startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = false;
startInfo.FileName = Path_of_the_exe;
startInfo.Arguments = Path_of_the_file;

using (Process exeProcess = Process.Start(startInfo))
{
   exeProcess.WaitForExit();
}

Launching cmd.exe like you are may explain the behaviour you are experiencing

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! It works. I did not know that i can use parameters without opening the cmd.exe explicitly.
I also cannot imagine a reason for launching "cmd", but those reasons seem to exists, e. g. see how Mads does it in his Image Optimizer VS extension. @madskvistkristensen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.