我有一个使用控制台的应用程序,但我更改了所有代码以写入文件而不是控制台.我现在希望控制台在运行应用程序时停止显示.我该怎么做呢?我不知道什么是首先打开控制台,即使代码中没有写入任何内容.
我查看了应用程序引用,但找不到正在引用的System.Console.我虽然禁用它会修复它或指出我正确的方向有错误.我不知道还能在哪里看.
我在网上找到的所有其他内容都是关于隐藏控制台的.我希望它不会出现在第一位.
转到"应用程序属性"并将"输出类型"从"控制台应用程序"更改为Windows
或者你可以使用下面的代码来完成它
using System.Runtime.InteropServices;
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
Run Code Online (Sandbox Code Playgroud)
并在主要
const int SW_HIDE = 0;
const int SW_SHOW = 5;
var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE); // To hide
ShowWindow(handle, SW_SHOW); // To show
Run Code Online (Sandbox Code Playgroud)
此外,您可以将应用程序作为服务运行.为此,您应该创建一个服务 - 文件 - >新项目 - > Visual C# - > Windows-> Windows服务.然后创建一个公共方法StartWork()并在那里添加所有逻辑.并调用此方法OnStart().
protected override void OnStart(string[] args)
{
try
{
this.StartJobs();
}
catch (Exception ex)
{
// catching exception
}
}
public void StartWork()
{
// all the logic here
}
Run Code Online (Sandbox Code Playgroud)
在main中,您应该创建此服务并使用System.ServiceProcess.ServiceBase.Run()它作为服务StartWork()运行或调用将其作为控制台应用程序运行.
static void Main(string[] args)
{
TestService = new TestService ();
#if DEBUG
TestService.StartWork()();
#else
System.ServiceProcess.ServiceBase.Run(TestService );
#endif
}
Run Code Online (Sandbox Code Playgroud)