简体   繁体   中英

To exit the console window in C# console application

I wanted to exit the console window as soon as my files are written in folder at back end but no matter whatever I try,

Environment.exit(0);
Environment.exit(1);
Environment.exit(-1);

also since I am executing from main method, I am returning the value, still my console window doesn't go off even after file are written to the destination folder,

below is my code,

 static void Main(string[] args)
    {
        string execute = ""; 
        execute = data_info_pooling(args[0], args[1], args[2]);
         Environment.Exit(0);
    }

Also I tried for using Application.exit(); but I am not able to get Application in drop-down box, I have explored almost all the possible helping links from stackoverflow and searched for any help but no idea where I am going wrong, I am trying to run this console application by opening the command prompt and then executing the command as below

Project Path\\Debug>"Project.exe" "First Parameter" " Second Parameter" "Third Parameter" ,

After file are written in the destination folder, console window waits and after pressing enter it just gives the path again to execute but I want window to exit as soon as task of writing file is completed,

Any help will be greatly appreciated,

-------Update Code--------

I have deleted for loop which is not necessary, actually I was wrong in application of my logic and apologize for my mistake, some of the below comments helped me to realize my mistake,it's finally Environment.exit();

which works, also I would like to give try to another answer of forming batch. Anyways...Thanks to all for yours valuable replies.

Environment.Exit and Application.Exit

Environment.Exit(0) is cleaner.

http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx

You can also change the return value from void to int . Then you can simply return a ExitCode .

 public static int Main(string[] args)
        {
            string execute = ""; 
                for (int i = 0; i < args.Length; i++)
                {
                    string argument = args[i];
                    execute = data_info_pooling(args[0], args[1], args[2]);
                }

           return -1
        }

As soon as your console application ends, the command window, created for it (assuming you run it from Windows Explorer ) will be closed automatically.

If, however, you open command window first, to specify parameters when running exe-file, then Explorer will not close it (obviously).

One possible solution is to wrap session into a batch file . Create project.bat with following content:

Project.exe "First Parameter" " Second Parameter" "Third Parameter"
; some other jobs

Running that batch file (from Windows Explorer directly) will pass parameters to you application and command window will be closed upon the end.

Is that what you want?

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