简体   繁体   English

HOWTO:关闭窗口命令提示符应用程序

[英]HOWTO: Close window command prompt application

I have a C# console application, let's say ConsoleApp.exe. 我有一个C#控制台应用程序,比方说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. 然后,应用程序启动,它执行一些操作,最后执行完我想要的窗口命令提示符(cmd),从中执行该命令以自动将其关闭。

I have tried Environment.Exit(0). 我曾尝试Environment.Exit(0)。 It finishes application by it does not close cmd window. 它完成由它的应用程序没有关闭cmd窗口。

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. 如果程序是从所谓的cmd ,它不应该关闭该命令程序。 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 如果您的应用程序正在向命令提示符下写入内容,请使用将args传递给它的快捷方式启动它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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