在 C# 控制台应用程序中隐藏控制台窗口

发布于 2024-09-25 19:36:17 字数 85 浏览 0 评论 0原文

问题是,我真的不希望显示控制台窗口,但解决方案应该正在运行。 我的观点是,我想让应用程序在后台运行,而不出现任何窗口。

我怎样才能做到这一点?

The thing is, I really don't want the console window to show up, but the solution should be running.
My point here is, I want to keep the application running in the background, without any window coming up.

How can I do that?

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

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

发布评论

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

评论(7

纸短情长 2024-10-02 19:36:17

将输出类型从控制台应用程序更改为Windows 应用程序。这可以在项目 -> 下完成属性-> Visual Studio 中的应用程序:

alt text

Change the output type from Console Application to Windows Application. This can be done under Project -> Properties -> Application in Visual Studio:

alt text

莫多说 2024-10-02 19:36:17

将您的应用程序类型更改为 Windows 应用程序。您的代码仍将运行,但它将没有控制台窗口,也没有标准 Windows 窗口,除非您创建一个窗口。

Change your application type to a windows application. Your code will still run, but it will have no console window, nor standard windows window unless you create one.

沉溺在你眼里的海 2024-10-02 19:36:17

最后,您可以使用 new ManualResetEvent(false).WaitOne() 来代替 Console.Readline/key 。这对我来说效果很好。

Instead of Console.Readline/key you can use new ManualResetEvent(false).WaitOne() at last. This works well for me.

ぺ禁宫浮华殁 2024-10-02 19:36:17

您可以使用 user32.dll 来实现此目的,无需 vscode

    class start
    {
        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        static void Main()
        {
            IntPtr h = Process.GetCurrentProcess().MainWindowHandle;
            ShowWindow(h, 0);
            // Do the rest

这仍然会闪烁屏幕,但效果足够好,

使用

using System.Runtime.InteropServices;
using System.Diagnostics;

注意我对 csharp 仍然是新手,并不完美,所以请随意发表评论和更正!

You can use user32.dll to achieve this without vscode

    class start
    {
        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        static void Main()
        {
            IntPtr h = Process.GetCurrentProcess().MainWindowHandle;
            ShowWindow(h, 0);
            // Do the rest

This still flashes the screen but works good enough

the usings are

using System.Runtime.InteropServices;
using System.Diagnostics;

Note im still new to csharp and not perfect so feel free to comment and corrections!

忘你却要生生世世 2024-10-02 19:36:17

也许您想尝试创建 Windows 服务应用程序。它将在后台运行,没有任何 UI。

Maybe you want to try creating a Windows Service application. It will be running in the background, without any UI.

小霸王臭丫头 2024-10-02 19:36:17

将输出类型从控制台应用程序更改为Windows应用程序

而不是Console.Readline/key 您可以在最后使用 new ManualResetEvent(false).WaitOne() 来保持应用程序运行。

Change the output type from Console Application to Windows Application,

And Instead of Console.Readline/key you can use new ManualResetEvent(false).WaitOne() at the end to keep the app running.

享受孤独 2024-10-02 19:36:17

在没有控制台窗口的情况下编译应用程序

csc.exe /target:winexe *.cs

To compile your application without a console window

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