在 C# 控制台应用程序中隐藏控制台窗口
问题是,我真的不希望显示控制台窗口,但解决方案应该正在运行。 我的观点是,我想让应用程序在后台运行,而不出现任何窗口。
我怎样才能做到这一点?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
将输出类型从控制台应用程序更改为Windows 应用程序。这可以在项目 -> 下完成属性-> Visual Studio 中的应用程序:
Change the output type from Console Application to Windows Application. This can be done under Project -> Properties -> Application in Visual Studio:
将您的应用程序类型更改为 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.
最后,您可以使用
new ManualResetEvent(false).WaitOne()
来代替Console.Readline/key
。这对我来说效果很好。Instead of
Console.Readline/key
you can usenew ManualResetEvent(false).WaitOne()
at last. This works well for me.您可以使用 user32.dll 来实现此目的,无需 vscode
这仍然会闪烁屏幕,但效果足够好,
使用
注意我对 csharp 仍然是新手,并不完美,所以请随意发表评论和更正!
You can use user32.dll to achieve this without vscode
This still flashes the screen but works good enough
the usings are
Note im still new to csharp and not perfect so feel free to comment and corrections!
也许您想尝试创建 Windows 服务应用程序。它将在后台运行,没有任何 UI。
Maybe you want to try creating a Windows Service application. It will be running in the background, without any UI.
将输出类型从控制台应用程序更改为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 usenew ManualResetEvent(false).WaitOne()
at the end to keep the app running.在没有控制台窗口的情况下编译应用程序
To compile your application without a console window