我正在使用一个小的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()时,控制台窗口仅保持可见;方法。没有它(和withoud p.ExitCode)控制台窗口关闭。我也尝试将StartInfo.WindowStyle设置为Hidden和Minimized,但两者都没有对窗口产生任何影响。
答案 0 :(得分:2)
只需将C#程序的输出类型更改为“Windows应用程序”,而不是“控制台应用程序”。如果您实际显示任何窗口,C#Windows应用程序并不在意。
答案 1 :(得分:1)
来自How to run a C# console application with the console hidden
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
}