简体   繁体   中英

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

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.

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 .

I really wanted to do it as Martin resolved, but I couldn't find editbin.exe. Well, I should have run the Visual Studio installer and installed Microsoft Visual C++, but I had limited memory, so I found another way of doing it NSubsys on NuGet .

The application will be made to run in hidden mode on publishing.

Note to copy the Npm install command from NuGet since searching it from package manager for solution didn't seem to find any results.

In the .NET Core SDK 2.1 (I have 2.1.500) this project file is working:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>net47</TargetFramework>
    </PropertyGroup>

</Project>

使用 .NET Core 3.0+(包括 .NET 5),只需在 csproj 中执行此操作:

<OutputType>WinExe</OutputType>

If you, like me, want to deploy an ASP.NET Core MVC application

  • on an internal network
  • don't wantIIS or WebHostService
  • don't care about Linux/Mac
  • and want to hide the Kestrel console from the user

then you should read Microsoft's official documentation on how to host an ASP.NET Core application in a Windows Service .

This is probably not the best or cleanest solution, but a trick I've used before. If you start a new Windows Forms application, then remove the reference to the form from the Program.cs file, it will run the application in the background with nothing showing up in the taskbar or on the desktop. It does appear in Task Manager though.

I used this trick to write an app to play a prank on a friend. It would run in the background and perform a system beep at random intervals. Again, probably not the most elegant solution, but one way of doing it.

I tried editing the .xproj file, but it seems it is ignored by Visual Studio/.NET.

I don't think it is currently supported as .NET was developed with web sites and console applications in mind. Microsoft has recently decided they will revert back to good old MSBuild. ( Changes to Project.json )

So, I think your best bet is to wait till then.

you can hide a .net core console application on runtime as soon it starts using windows api call. see the sample code below

using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace HideConsoleApp
{
    class Program
    {
        [DllImport("Kernel32.dll")]
        private static extern IntPtr GetConsoleWindow();
        [DllImport("User32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int cmdShow);
        static void Main(string[] args)
        {
            Console.WriteLine("press any key to hide console window");
            Console.ReadKey();
            VsConsoleWindow(0);
            Console.ReadKey();
        }

        public static void VsConsoleWindow(int i)
        {
           
            IntPtr hWnd = GetConsoleWindow();
            if (hWnd != IntPtr.Zero)
            {
                // value of cv = 0 hide console window  
                // value of cv = 1 show console window
                ShowWindow(hWnd, i);
                Thread.Sleep(5000);
                ShowWindow(hWnd, 1);
            }
            return;
        }



    }
}

https://github.com/robinsondominic/hide.net-core-console-application

You're looking for the ProcessStartInfo class. Specifically the CreateNoWindow Property.

ProcessStartInfo processInfo = newProcessStartInfo("Name", "args");
processInfo.CreateNoWindow = true;

The class name and property vary a little based on the version on .NET you're using. It is also important to note that this needs to be populated and called before the process is initialized. This works when the program in question is being called programmatically or from PowerShell scripts, etc.

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