如何在 C# 中隐藏控制台应用程序

发布于 2024-08-31 05:10:58 字数 53 浏览 4 评论 0原文

我有一个 C# 控制台应用程序,我希望用户看不到它。

我怎样才能做到这一点?

I have a console application in C#, and I want that the user won't be able to see it.

How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

燃情 2024-09-07 05:10:58

将其编译为 Windows 窗体应用程序。如果您没有显式打开任何 Windows,它将不会显示任何 UI。

Compile it as a Windows Forms application. Then it won't display any UI, if you do not explicitly open any Windows.

鱼忆七猫命九 2024-09-07 05:10:58

在 ProjectProperties 中将输出类型设置为 Windows 应用程序。

On ProjectProperties set Output Type as Windows Application.

瑾夏年华 2024-09-07 05:10:58

听起来您不需要控制台应用程序,而是需要一个不打开(可见)窗口的 Windows GUI 应用程序。

Sounds like you don't want a console application, but a windows GUI application that doesn't open a (visible) window.

半步萧音过轻尘 2024-09-07 05:10:58

使用以下代码创建一个控制台应用程序“MyAppProxy”,并将 MyAppProxy 放入启动目录中,

public static void main(string[] args)
{
   Process p = new Process("MyApp");
   ProcessStartUpInfo pinfo = new ProcessStartUpInfo();
   p.StartupInfo = pinfo;
   pinfo.CreateNoWindow = true;
   pinfo.ShellExecute = false;

   p.RaiseEvents = true;

   AutoResetEvent wait = new AutoResetEvent(false);
   p.ProcessExit += (s,e)=>{ wait.Set(); };

   p.Start();
   wait.WaitOne();
}

您可能需要在这里修复某些项目,因为我没有检查代码的正确性,它可能无法编译,因为某些属性名称可能不同,但希望你明白了。

Create a console application "MyAppProxy" with following code, and put MyAppProxy in start up dir,

public static void main(string[] args)
{
   Process p = new Process("MyApp");
   ProcessStartUpInfo pinfo = new ProcessStartUpInfo();
   p.StartupInfo = pinfo;
   pinfo.CreateNoWindow = true;
   pinfo.ShellExecute = false;

   p.RaiseEvents = true;

   AutoResetEvent wait = new AutoResetEvent(false);
   p.ProcessExit += (s,e)=>{ wait.Set(); };

   p.Start();
   wait.WaitOne();
}

You may need to fix certain items here as I didnt check correctness of the code, it may not compile because some property names may be different, but hope you get the idea.

迷迭香的记忆 2024-09-07 05:10:58

最好的方法是在没有窗口的情况下启动该进程。

        Process p = new Process();
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.Arguments = "echo Hello!";
        //either..
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        //or..
        p.StartInfo.CreateNoWindow = true;
        p.Start();

查看其他可能的解决方案 -

Toggle Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden at运行时

引入另一个进程当 ShowInTaskbar = false 时窗口转到前台

The best way is to start the process without window.

        Process p = new Process();
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.Arguments = "echo Hello!";
        //either..
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        //or..
        p.StartInfo.CreateNoWindow = true;
        p.Start();

See other probable solutions -

Toggle Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden at runtime

and,

Bring another processes Window to foreground when it has ShowInTaskbar = false

罗罗贝儿 2024-09-07 05:10:58

要在没有其他功能时隐藏 C# 中的控制台应用程序,请使用以下代码:

[DllImport("kernel32.dll")]
public static extern bool FreeConsole();

将 FreeConsole() 放置在代码中的任何位置,我将其放置在 Init() 中,然后隐藏命令行。

To hide a console applicatin in C# when nothing else works use this code:

[DllImport("kernel32.dll")]
public static extern bool FreeConsole();

Place FreeConsole() anywhere in the code, I placed it in the Init(), and the commandline is hidden.

最单纯的乌龟 2024-09-07 05:10:58

您可以 Pinvoke 对 FindWindow() 的调用来获取窗口的句柄,然后调用 ShowWindow() 来隐藏窗口
或者
使用 ProcessStartInfo.CreateNoWindow 从另一个应用程序启动您的应用程序

You can Pinvoke a call to FindWindow() to get a handle to your window and then call ShowWindow() to hide the window
OR
Start your application from another one using ProcessStartInfo.CreateNoWindow

神爱温柔 2024-09-07 05:10:58

我有一个通用的解决方案可以分享:

using System;
using System.Runtime.InteropServices;

namespace WhateverNamepaceYouAreUsing
{
    class Magician
    {
    [DllImport("kernel32.dll")]
        static extern IntPtr GetConsoleWindow();

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        const int HIDE = 0;
        const int SHOW = 5;

        public static void DisappearConsole()
        {
            ShowWindow(GetConsoleWindow(), HIDE);
        }
    }
}

只需将此类包含在您的项目中,然后调用 Magician.DisappearConsole(); 即可。

当您通过单击启动程序时,控制台会闪烁。从命令提示符执行时,命令提示符在执行后很快就会消失。

我为一个 Discord Bot 执行此操作,它作为一个不可见的进程永远在我的计算机后台运行。这比让 TopShelf 为我工作更容易。在我在其他地方找到的代码的帮助下写这篇文章之前,一些 TopShelf 教程让我失败了。 ;P

我也尝试简单地更改 Visual Studio 中的设置 >项目>>属性>应用程序作为 Windows 应用程序而不是控制台应用程序启动,而我的项目的某些内容阻止了它隐藏我的控制台 - 也许是因为 DSharpPlus 要求在启动时启动控制台。我不知道。不管出于什么原因,这个类允许我在控制台弹出后轻松地杀死它。

希望这位魔术师能帮助别人。 ;)

I've got a general solution to share:

using System;
using System.Runtime.InteropServices;

namespace WhateverNamepaceYouAreUsing
{
    class Magician
    {
    [DllImport("kernel32.dll")]
        static extern IntPtr GetConsoleWindow();

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        const int HIDE = 0;
        const int SHOW = 5;

        public static void DisappearConsole()
        {
            ShowWindow(GetConsoleWindow(), HIDE);
        }
    }
}

Just include this class in your project, and call Magician.DisappearConsole();.

A console will flash when you start the program by clicking on it. When executing from the command prompt, the command prompt disappears very shortly after execution.

I do this for a Discord Bot that runs forever in the background of my computer as an invisible process. It was easier than getting TopShelf to work for me. A couple TopShelf tutorials failed me before I wrote this with some help from code I found elsewhere. ;P

I also tried simply changing the settings in Visual Studio > Project > Properties > Application to launch as a Windows Application instead of a Console Application, and something about my project prevented this from hiding my console - perhaps because DSharpPlus demands to launch a console on startup. I don't know. Whatever the reason, this class allows me to easily kill the console after it pops up.

Hope this Magician helps somebody. ;)

独享拥抱 2024-09-07 05:10:58

创建一个 wcf 服务并根据您的需要托管它。

Create a wcf service and host it as per your need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文