4

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

shi
  • 41
  • 1
  • 1
  • 2
  • What is the command you're using to start the application? [This may help](http://stackoverflow.com/questions/18738490/why-the-cmd-window-will-not-close-after-batch-file-execution?rq=1) – Sriram Sakthivel Jul 17 '14 at 06:58
  • 1
    Check http://stackoverflow.com/questions/2531837/how-can-i-get-the-pid-of-the-parent-process-of-my-application – Yuval Itzchakov Jul 17 '14 at 06:59
  • you may find your answer here . http://stackoverflow.com/questions/5182824/execute-command-line-using-c-sharp – varsha Sep 11 '14 at 08:49
  • Just confirming, you'd like to have `app.exe` do the same as a batch file: `@echo off\n start "app.exe" app.exe\n exit\n ` (`\n` because we can't have newlines in comments)? – Mark Hurd Jan 07 '15 at 14:03

2 Answers2

9

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;
Arthur
  • 2,601
  • 1
  • 22
  • 19
1

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

Ateeq
  • 797
  • 2
  • 9
  • 27