views:

35

answers:

2

Hello!

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!

+1  A: 

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.

rerun
The Kill method worked, it closed the Explorer window and it didn´t start it back, but i would like to know if there is a less critical way to close the Explorer windows.Thanks!
MorgoZ
What are you attempting to accomplish
rerun