MorgoZ
MorgoZ

Reputation: 181

Problems with CloseMainWindow() to close a Windows Explorer window

I´m facing a problem when trying to close a Windows Explorer (not Internet Explorer) window through another application, using the "Process.CloseMainWindow()" method; because it doesn´t close the Explorer window, it tries to close the full Windows (Operative System), by the way, Windows XP.

The code is as follows:

[DllImport("user32.dll")]
    static extern int GetForegroundWindow();

    [DllImport("user32.dll")]
    private static extern UInt32 GetWindowThreadProcessId(Int32 hWnd, out Int32 lpdwProcessId);


    public String[] exeCommand()
    {

        try
        {
            //Get App
            Int32 hwnd = 0;
            hwnd = GetForegroundWindow();
            Process actualProcess = Process.GetProcessById(GetWindowProcessID(hwnd));

            //Close App
            if (!actualProcess.CloseMainWindow())
                actualProcess.Kill();

        }
        catch { throw; }

        return null;

    }

Suppose that the "actualProcess" is "explorer.exe"

Any help will be appreciated!! Salutes!

Upvotes: 1

Views: 1427

Answers (1)

rerun
rerun

Reputation: 25505

I believe this is because the main window for explore is considered the shell. You can however kill the process, but windows will start it right back up.

Upvotes: 1

Related Questions