Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

windows

cmd

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

like image 766
shi Avatar asked Feb 12 '23 11:02

shi


1 Answers

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;
like image 117
Arthur Avatar answered Feb 15 '23 10:02

Arthur