隐藏控制台窗口

本文关键字:窗口 控制台 隐藏 | 更新日期: 2023-09-27 18:00:49

我正在使用一个小的C#可执行文件来启动一个java jar。我想恢复jar返回的退出代码,以便在需要时重新启动jar。然而,c#应用程序一直显示一个黑色控制台窗口,我无法摆脱它,有人知道如何解决这个问题吗?我正在使用下面的C#代码来启动进程

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "jre/bin/java.exe";
p.StartInfo.Arguments = "-Djava.library.path=bin -jar readermanager.jar";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.waitForExit();
return p.ExitCode;

控制台窗口仅在使用waitForExit((时保持可见;方法如果没有它(以及没有p.ExitCode(,控制台窗口将关闭。我还尝试将StartInfo.WindowStyle设置为"隐藏"answers"最小化",但两者都对窗口没有任何影响。

隐藏控制台窗口

只需将C#程序的输出类型更改为"Windows应用程序",而不是"控制台应用程序"。一个C#Windows应用程序并不关心你是否真的显示了任何窗口。

如何在控制台隐藏的情况下运行C#控制台应用程序

System.Diagnostics.ProcessStartInfo start =
  new System.Diagnostics.ProcessStartInfo();     
start.FileName = dir + @"'Myprocesstostart.exe";
start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

但是,如果这不起作用,那么:http://social.msdn.microsoft.com/Forums/eu/csharpgeneral/thread/ea8b0fd5-a660-46f9-9dcb-d525cc22dcbd

using System.Runtime.InteropServices;
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

IntPtr hWnd = FindWindow(null, "Window caption here");
if(hWnd != IntPtr.Zero)
{
    //Hide the window
    ShowWindow(hWnd, 0); // 0 = SW_HIDE
}

if(hWnd != IntPtr.Zero)
{
   //Show window again
   ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA
}