How to close a cmd window after I ran an exe. file from it?

14,332

Solution 1

In Windows Command prompt it's "&" to attach commands in one line. So it'll be:

process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/k " + Command + " & exit";

But if you read the "cmd /?", you'll see that the purpose of "/k" argument is to keep the window. So if it's not what you want, just use the "/c" argument instead.

process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c " + Command;

Solution 2

try this

Process process = new Process();
process.StartInfo.Arguments = Command + "; exit";

the ";" is to separate between the commands and the "exit" is the next command to execute

Share:
14,332
Admin
Author by

Admin

Updated on June 21, 2022

Comments

  • Admin
    Admin almost 2 years

    I have a c# app (app.exe), which I want to run from a command line window and then to CLOSE the command line window after the app started.

    I tried to search for "cmd" in the processes list and to close it (cmdProcess.CloseMainWindow()) but in this case, if I run app.exe by double-click only, and there is another cmd opened separately, it will be closed- and I don't want that. How can I know which cmd instance runs my app?

    thanks