Question

I am trying to use AppWinStyle with Process.start but it does not work as i expected. The below code will open file 1.txt and it will be minimized with no focus to it.

Shell("notepad D:\1.txt", AppWinStyle.MinimizedNoFocus)

But below code will open 1.txt with focus on it.It will not be minimized.

Process.Start("D:\1.txt", AppWinStyle.MinimizedNoFocus)

Why does it happen? Please help me to solve this

Was it helpful?

Solution

A boilerplate example:

using System;
using System.Diagnostics;

class Program {
    static void Main(string[] args) {
        var psi = new ProcessStartInfo("notepad.exe");
        //psi.WindowStyle = ProcessWindowStyle.Hidden;
        Process.Start(psi);
    }
}

Run it once to make sure it works, then remove the comment and observe the outcome. You'll briefly see the Notepad window flash but it immediately terminates again. Might depend on the Windows version, I described what happened for Windows 8.1

A GUI app gets the ProcessWindowStyle you specify through its WinMain() entry point, the nCmdShow argument passes the value you specified. What the app actually does with that value is entirely up to the app. Boilerplate implementation is to pass it to the ShowWindow() call, the one that makes its main window visible.

Using ProcessWindowStyle.Hidden is in general very problematic and a properly written GUI app will ignore it, like Notepad did. Because what you asked it to do is to start the program but not display any window, not even a taskbar button. In other words, you asked it to turn into a zombie, a process that runs without any way for the user to get to it. The only possible thing the user could do is run Task Manager and kill the process.

So sure, definitely expect this to not work. It shouldn't.

OTHER TIPS

No such overload with Process.Start:

Process.Start("D:\1.txt", AppWinStyle.MinimizedNoFocus)

See all overloads here: Process.Start Method

In order to achieve it using the Process.Start, use the ProcessStartInfo.WindowStyle, setting it to ProcessWindowStyle.Minimized.

By the way, the AppWinStyle enumerator is specific for the Shell function:

Indicates the window style to use for the invoked program when calling the Shell function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top