简体   繁体   中英

HOWTO: Close window command prompt application

I have a C# console application, let's say ConsoleApp.exe. It is executed from command prompt and I pass it some parameters, eg:

ConsoleApp.exe arg1 arg2 -opt1 option1 -opt2 option2

Then application starts, it does some stuff and finally after doing all the stuff I want window command prompt (cmd) from which it is executed to be closed automatically.

I have tried Environment.Exit(0). It finishes application by it does not close cmd window.

So how can I do this programatically?

You should not do that. If the program was called from cmd , it should not close the command program. This would be really wrong if the user intends to carry on with their work after your program has ran.

You can of course look up the parent process and kill that one when you want to exit your application, but please don't.

You are not suppose to kill the parent program. If your app is writing something to the command prompt then start it using a shortcut passing your args to it

"c:\path\to\exe\ConsoleApp.exe" arg1 arg2 -opt1 option1 -opt2 option2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM