shi
shi

Reputation: 41

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

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

Upvotes: 4

Views: 19240

Answers (2)

Arthur
Arthur

Reputation: 2639

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;

Upvotes: 9

Ateeq
Ateeq

Reputation: 823

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

Upvotes: 1

Related Questions