我有一个正在使用控制台的应用程序,但是我更改了所有代码以写入文件而不是控制台。现在,我希望控制台在运行应用程序时不再出现。我该怎么做呢?我不知道是什么首先打开了控制台,即使代码中没有写入任何内容。
我已经查看了应用程序引用,但找不到被引用的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);
而在主要
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
另外,您可以将应用程序作为服务运行。为此,您应该创建一个服务-File-> New Project-> Visual C#-> Windows-> Windows Service。然后创建一个公共方法,StartWork()
并在其中添加所有逻辑。并在中调用此方法OnStart()
。
protected override void OnStart(string[] args)
{
try
{
this.StartJobs();
}
catch (Exception ex)
{
// catching exception
}
}
public void StartWork()
{
// all the logic here
}
首先,您应该创建此服务并将System.ServiceProcess.ServiceBase.Run()
其用作服务运行,或调用StartWork()
以将其作为控制台应用程序运行。
static void Main(string[] args)
{
TestService = new TestService ();
#if DEBUG
TestService.StartWork()();
#else
System.ServiceProcess.ServiceBase.Run(TestService );
#endif
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句