Ste*_*ger 16 .net c# vb.net console winapi
问题:我有一个不应该看到的控制台程序.(它重置IIS并删除临时文件.)
现在我可以设法在开始后立即隐藏窗口,如下所示:
static void Main(string[] args)
{
var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
Console.WriteLine(currentProcess.MainWindowTitle);
IntPtr hWnd = currentProcess.MainWindowHandle;//FindWindow(null, "Your console windows caption"); //put your console window caption here
if (hWnd != IntPtr.Zero)
{
//Hide the window
ShowWindow(hWnd, 0); // 0 = SW_HIDE
}
Run Code Online (Sandbox Code Playgroud)
问题是这显示了一秒钟的窗口.是否有控制台程序的构造函数,我可以在窗口显示之前隐藏它?
第二个:
我用
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
Run Code Online (Sandbox Code Playgroud)
而且我不喜欢它里面的32.没有DllImport有没有办法做到这一点?
一种.NET方式?
Ric*_*ard 32
如果您不需要控制台(例如for Console.WriteLine
),则将应用程序构建选项更改为Windows应用程序.
这会更改.exe
标头中的标志,以便Windows在应用程序启动时不会分配控制台会话.
如果我理解您的问题,只需手动创建控制台进程并隐藏控制台窗口:
Process process = new Process();
process.StartInfo.FileName = "Bogus.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
Run Code Online (Sandbox Code Playgroud)
我为WPF应用程序执行此操作,该应用程序执行控制台应用程序(在后台工作程序中)并重定向标准输出,以便您可以在需要时在窗口中显示结果.如果您需要查看更多代码(工作人员代码,重定向,参数传递等),请告诉我们.
归档时间: |
|
查看次数: |
19692 次 |
最近记录: |