Hide System.Diagnostics.Process GUI window
Question
I'm really struggling with the following problem. I know that there are similar questions to my scenario but non of them are exactly the same scenario.
I've created an SSIS custom component that starts an external application that I want to run in silent mode. No matter what I try the gui always appears. If I use the same code in a console application I don't run into any issues at all. The only difference I can see between my SSIS custom component (dll) and the console application is that my component references System.Windows.Forms and uses BIDS. I'm using the following code. Any tips I'm more than willing to try.
Process winscp = new Process();
winscp.StartInfo.FileName = @stExe;
winscp.StartInfo.Arguments = "/log=";
winscp.StartInfo.UseShellExecute = false;
winscp.StartInfo.RedirectStandardInput = true;
winscp.StartInfo.RedirectStandardOutput = true;
winscp.StartInfo.CreateNoWindow = true;
winscp.Start();
Solution
Try to find the window of the new process, hide it as soon as possible.
Use Win32 API,
HWND hwnd = FindWindow(NULL, "Title");
ShowWindow(hwnd, SW_HIDE);
OTHER TIPS
It is possible using ShowWindow. You need to make sure that the method ShowWindow is called after the process window has been created and not during it's startup.
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
Call the following after the process window appears.
ShowWindow(winscp.MainWindowHandle, 0)
As you are running WinSCP:
- If using the
winscp.com
, theCreateNoWindow
should work as the console window is created by the system, hence controlable by theCreateNoWindow
- If using the
winscp.exe
, theCreateNoWindow
does not work as the console windows is created by WinSCP itself. But you can force WinSCP not creating the window by omitting the/console
argument
For details refer to:
https://winscp.net/eng/docs/executables