简体   繁体   English

是否可以静默运行 .NET Core 控制台应用程序(隐藏控制台窗口)?

[英]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.我正在尝试为自己自动执行一些任务,并且我编写了一些 .NET Core 1.0 控制台应用程序。 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.其中之一是 BrowserRouter - 一个简单的应用程序,它基于 URL 模式,决定在我单击 HTTP(S) 链接时打开哪个浏览器/浏览器配置文件。

That works fine, but there is always the console window which appears and immediately disappears.这工作正常,但总有控制台 window 出现并立即消失。

Is there a way to run the .NET Core console application silently (hiding the console window)?有没有办法静默运行 .NET Core 控制台应用程序(隐藏控制台窗口)?

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.我知道在完整的 .NET Framework 中,可以将 output 类型更改为Windows Application ,但这对于 .NET Core 不可用(还?)。

Update 2018 : In .NET Core 3.0+, you will be able to set <OutputType>WinExe</OutputType> inside of the csproj file. 2018更新:在 .NET Core 3.0+ 中,您将能够在 csproj 文件中设置<OutputType>WinExe</OutputType>

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.目前,这不是直接可用的,因为 Windows EXE 文件包含一个标志,表明它是 Windows 甚至在启动应用程序之前评估的“控制台”或“GUI”应用程序。 However, this flag can be changed using editbin.exe (ships with Visual Studio' C++ build tools as far as I know).但是,可以使用editbin.exe更改此标志(据我所知,它随 Visual Studio 的 C++ 构建工具一起提供)。

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 .在此基础上,您可以创建一个自包含的 .NET Core 应用程序(从project.json删除"type": "platform"并添加一个运行时部分),以便您的项目输出包含一个实际的.exe文件并添加一个帖子- 构建调用editbin.exe /subsystem:windows yourapp.exe

I really wanted to do it as Martin resolved, but I couldn't find editbin.exe.我真的很想在 Martin 解决后这样做,但我找不到 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 .好吧,我应该运行 Visual Studio 安装程序并安装 Microsoft Visual C++,但我的内存有限,所以我在NuGet上找到了另一种 NSubsys 方法。

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.请注意从 NuGet 复制 Npm install 命令,因为从包管理器中搜索解决方案似乎没有找到任何结果。

In the .NET Core SDK 2.1 (I have 2.1.500) this project file is working:在 .NET Core SDK 2.1(我有 2.1.500)中,这个项目文件正在运行:

<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如果您像我一样想要部署ASP.NET Core MVC 应用程序

  • on an internal network在内部网络上
  • don't wantIIS or WebHostService不想要IISWebHostService
  • don't care about Linux/Mac不在乎 Linux/Mac
  • and want to hide the Kestrel console from the user并希望对用户隐藏 Kestrel 控制台

then you should read Microsoft's official documentation on how to host an ASP.NET Core application in a Windows Service .那么您应该阅读 Microsoft 的官方文档,了解如何在 Windows 服务中托管 ASP.NET Core 应用程序

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.如果您启动一个新的 Windows 窗体应用程序,然后从 Program.cs 文件中删除对该窗体的引用,它将在后台运行该应用程序,而任务栏或桌面上不会显示任何内容。 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.我尝试编辑.xproj文件,但它似乎被 Visual Studio/.NET 忽略了。

I don't think it is currently supported as .NET was developed with web sites and console applications in mind.我认为目前不支持它,因为 .NET 是在考虑网站和控制台应用程序的情况下开发的。 Microsoft has recently decided they will revert back to good old MSBuild.微软最近决定他们将恢复到旧的 MSBuild。 ( Changes to Project.json ) 对 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.您可以在运行时隐藏 .net 核心控制台应用程序,只要它开始使用 windows api 调用。 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 https://github.com/robinsondominic/hide.net-core-console-application

You're looking for the ProcessStartInfo class.您正在寻找ProcessStartInfo类。 Specifically the CreateNoWindow Property.特别是 CreateNoWindow 属性。

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.类名和属性根据您使用的 .NET 版本略有不同。 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.当以编程方式或从 PowerShell 脚本等调用相关程序时,此方法有效。

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

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