I recently needed to launch a Windows Forms application from code, but I needed that application to be invisible to the user. I didn’t want anything to show up in the taskbar or any short flicker on the screen while the application launches. I started looking at the ProcessStartInfo properties to see how to accomplish this and found the WindowStyle property, which looked like just what I needed. I was quickly frustrated though as it didn’t seem to be working:
- Process process = new Process
- {
- EnableRaisingEvents = true,
- StartInfo =
- {
- CreateNoWindow = false,
- FileName = "iexplore.exe",
- UseShellExecute = false,
- ErrorDialog = false,
- WindowStyle = ProcessWindowStyle.Hidden
- }
- };
- process.Exited += process_Exited;
- process.Start();
You would think that the application would be hidden, but no! Instead, it shows up just as if I had launched it normally. As it turns out, you need:
- UseShellExecute = true,
Why? I’m not quite sure. The documentation for UseShellExecute doesn’t give any helpful clues. Of course, UseShellExecute is true by default so maybe most developers don’t run into this problem. However, the lack of documentation is sure to lead to many developers being confused who do go down this path. There may be other dependencies too, but I haven’t run into any others, yet.