让我的控制台应用程序不可见
我正在为我的公共图书馆开发一个控制台应用程序作为学校项目。控制台应用程序将在用户登录后立即运行并执行一些后台工作。
问题是,我不希望控制台应用程序实际出现。我需要它看不见。我最不需要的就是抱怨,因为有些人对 CMD 窗口的打开和关闭感到害怕,此外图书馆希望它尽可能不可见。
我尝试按照该线程中的代码进行操作: http://social.msdn .microsoft.com/Forums/en-US/csharpgeneral/thread/ea8b0fd5-a660-46f9-9dcb-d525cc22dcbd
但无济于事,我仍然可以看到控制台应用程序在完成所有操作后弹出打开和关闭它的工作。
有没有更好的方法来阻止控制台出现?谢谢。
I am developing a console application for my public library as a school project. The console application will run as soon as the user logs on and do some background work.
The thing is, I don't want the console application to actually appear. I need it invisible. The last thing I need is complaints because some people got freaked out that a CMD window opened and closed, besides that the library wants it as invisible as possible.
I tried following the code in this thread:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ea8b0fd5-a660-46f9-9dcb-d525cc22dcbd
but to no avail, I can still see the console application pop open and close after it has done all of its work.
Is there a better way to stop the console from appearing? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
最好的办法就是不要将其编译为控制台应用程序!将其编译为 Windows EXE,不会显示任何控制台。然后您可以在 Main 方法中执行您需要执行的任何操作,而无需显示 UI。
但无论如何,如果您必须隐藏/显示控制台窗口,我会避免使用 FindWindow 来完成此任务,因为有一个更可靠的 API:GetConsoleWindow。这将为您提供控制台窗口的 HWND,您可以尝试将其传递给 ShowWindow。
The best thing to do is just don't compile it as a console application! Compile it as a Windows EXE and no console will show. Then you can just do whatever you need to do in the Main method without showing a UI.
But in any case, if you must hide/show the console window I would steer clear of using FindWindow for this task since there is a much more reliable API for this: GetConsoleWindow. This will give you the HWND of the console window and you can try passing that to ShowWindow.
正如 Josh Einstein 建议您可以使用 ShowWindow Api 用于隐藏窗口。
这是一个例子:
我不确定这段代码,因为我还没有测试过它。如果您遇到任何问题,请告诉我。
As Josh Einstein has suggested you can use ShowWindow Api to hide your window.
Here's a example:
I am not sure about this code as I have not tested it. Let me know if you face any problem.
您是否尝试过:项目属性>应用>>输出类型:到“Windows 应用程序”?
Have you tried: Project Properties> Application > output Type: to "Windows Application"?
它比控制台应用程序稍微复杂一些...但是如果您希望有人登录时真正在后台运行某些东西,那么您可以创建一个 Windows 服务应用程序。
但它确实需要一些额外的工作来设置和安装 Windows 服务,但网上有大量的示例代码:
http://msdn.microsoft.com/en-us/library/9k985bc9(v=VS.80).aspx
http://msdn.microsoft.com/en-us/库/sd8zc8ha(v=VS.80).aspx
http://www.c-sharpcorner.com/uploadfile/mahesh/window_service11262005045007am/window_service.aspx
http://www.developer.com/net/net/article.php/2173801/Creating-a-Windows-Service-in -NET.htm
http://www.codeproject.com/KB/ dotnet/simplewindowsservice.aspx
Its a little more complicated than a console application... but if you want something to truly be running in the background when someone logs in then you could create a Windows Service application.
But it does require a little additional work in setting up and installing the Windows Service but there is an abundance of example code on the web:
http://msdn.microsoft.com/en-us/library/9k985bc9(v=VS.80).aspx
http://msdn.microsoft.com/en-us/library/sd8zc8ha(v=VS.80).aspx
http://www.c-sharpcorner.com/uploadfile/mahesh/window_service11262005045007am/window_service.aspx
http://www.developer.com/net/net/article.php/2173801/Creating-a-Windows-Service-in-NET.htm
http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx
您好,我正在创建一个由任务计划程序调用的控制台应用程序。我不想显示控制台应用程序,因此我更改了项目属性以将输出输出到 Windows 应用程序。
将输出类型更改为 Windows 应用程序
转到:项目 -> 项目属性
并将输出类型更改为Windows应用程序
Hello I was creating a Console application to be called by the task scheduler. I didn't want the console app to show up so I changed the project properties to have the output to Windows Application.
Change the output type to Windows application
Go to : Project - >Project Properties
And change the output type to Windows Application
我尝试了两种方法 2) Searock,然后 1) Josh --- 使用 Searock 的解决方案,控制台应用程序窗口仍然出现,尽管时间非常短暂 --- 但是,使用 Josh 的解决方案,控制台没有出现,我的程序也没有任何问题-- 当然,我确实必须将所有 console.writeline 调用替换为将信息记录到日志文件中的调用
注意:我刚刚对 Josh 的解决方案发表了评论,但我还不能这样做:)
I tried both methods 2) Searock and then 1) Josh --- with Searock's solution the console app window still appeared, although for a very brief moment --- however with Josh's solution the console did not appear nor did my program have any issues -- of course I did have to replace all the console.writeline calls with a call that logged the information out to a log file
Note: I would have just commented on Josh's solution but I cannot do that quite yet :)