简体   繁体   中英

Hide console window

I'm using a small C# executable to launch a java jar. I want to recover the exit code returned by the jar so i can restart the jar if i need to. However the c# application keeps showing a black console window and i can't get rid of it, does anyone know how to fix this? I'm using t he following C# code to start the process

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;

The console window only keeps visible when using the waitForExit(); method. Without it (and withoud p.ExitCode) the console windows closes. I also tried setting the StartInfo.WindowStyle to Hidden and Minimized but both don't have any effect on the window.

Just change the output type of your C# program to be a "Windows Application" instead of a "Console Application". AC# Windows application doesn't really care if you actually display any windows.

From 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;

But if that does not work, how about this: 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM