如何在 C# 中隐藏控制台应用程序
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
将其编译为 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.
在 ProjectProperties 中将输出类型设置为 Windows 应用程序。
On ProjectProperties set Output Type as Windows Application.
听起来您不需要控制台应用程序,而是需要一个不打开(可见)窗口的 Windows GUI 应用程序。
Sounds like you don't want a console application, but a windows GUI application that doesn't open a (visible) window.
使用以下代码创建一个控制台应用程序“MyAppProxy”,并将 MyAppProxy 放入启动目录中,
您可能需要在这里修复某些项目,因为我没有检查代码的正确性,它可能无法编译,因为某些属性名称可能不同,但希望你明白了。
Create a console application "MyAppProxy" with following code, and put MyAppProxy in start up dir,
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.
最好的方法是在没有窗口的情况下启动该进程。
查看其他可能的解决方案 -
Toggle Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden at运行时
,
引入另一个进程当 ShowInTaskbar = false 时窗口转到前台
The best way is to start the process without window.
See other probable solutions -
Toggle Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden at runtime
and,
Bring another processes Window to foreground when it has ShowInTaskbar = false
要在没有其他功能时隐藏 C# 中的控制台应用程序,请使用以下代码:
将 FreeConsole() 放置在代码中的任何位置,我将其放置在 Init() 中,然后隐藏命令行。
To hide a console applicatin in C# when nothing else works use this code:
Place FreeConsole() anywhere in the code, I placed it in the Init(), and the commandline is hidden.
您可以 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
我有一个通用的解决方案可以分享:
只需将此类包含在您的项目中,然后调用 Magician.DisappearConsole(); 即可。
当您通过单击启动程序时,控制台会闪烁。从命令提示符执行时,命令提示符在执行后很快就会消失。
我为一个 Discord Bot 执行此操作,它作为一个不可见的进程永远在我的计算机后台运行。这比让 TopShelf 为我工作更容易。在我在其他地方找到的代码的帮助下写这篇文章之前,一些 TopShelf 教程让我失败了。 ;P
我也尝试简单地更改 Visual Studio 中的设置 >项目>>属性>应用程序作为 Windows 应用程序而不是控制台应用程序启动,而我的项目的某些内容阻止了它隐藏我的控制台 - 也许是因为 DSharpPlus 要求在启动时启动控制台。我不知道。不管出于什么原因,这个类允许我在控制台弹出后轻松地杀死它。
希望这位魔术师能帮助别人。 ;)
I've got a general solution to share:
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. ;)
创建一个 wcf 服务并根据您的需要托管它。
Create a wcf service and host it as per your need.