Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run a .NET Core console application silently (hide console window)?

Tags:

c#

.net-core

I'm trying to automate some tasks for myself and I wrote a few .NET Core 1.0 console applications. One of them is BrowserRouter - a simple application which, based on a URL pattern, decides which browser / browser profile to open when I click on HTTP(S) links.

That works fine, but there is always the console window which appears and immediately disappears.

Is there a way to run the .NET Core console application silently (hiding the console window)?

I know in the full .NET Framework it is possible to change the output type to Windows Application, but that's not available (yet?) for .NET Core.

like image 506
radek.pribyl Avatar asked Aug 04 '16 20:08

radek.pribyl


People also ask

How do I run a console application without showing the console?

If you do not know what I am talking about: press Win+R, type “help” and press ENTER. A black console window will open, execute the HELP command and close again. Often, this is not desired. Instead, the command should execute without any visible window.

How do I run a .NET core console application on Windows?

You can do it right in Visual Studio. Right click the Console App Project and select Publish. Then change Deployment Mode to Self-contained or Framework dependent. . NET Core 3.0 introduces a Single file deployment which is a single executable.

How do I run a console application in Visual Studio?

Build and run your code in Visual Studio To run the code, on the menu bar, choose Debug, Start without debugging. A console window opens and then runs your app. When you start a console app in Visual Studio, it runs your code, then prints "Press any key to continue . . ." to give you a chance to see the output.

How do I stop a console window from closing in C#?

Try Ctrl + F5 in Visual Studio to run your program, this will add a pause with "Press any key to continue..." automatically without any Console. Readline() or ReadKey() functions.


1 Answers

Update 2018: In .NET Core 3.0+, you will be able to set <OutputType>WinExe</OutputType> inside of the csproj file.

Currently, this is not directly possible out of the box because Windows EXE files contain a flag indicating if it is a "console" or "GUI" application that Windows evaluates even before starting the application. However, this flag can be changed using editbin.exe (ships with Visual Studio' C++ build tools as far as I know).

Building on this, you could create a self-contained .NET Core application (removing the "type": "platform" from the project.json and adding a runtimes section) so your project output includes an actual .exe file and add a post-build script that invokes editbin.exe /subsystem:windows yourapp.exe.

like image 111
Martin Ullrich Avatar answered Sep 25 '22 15:09

Martin Ullrich