How to close specific file explorer in C#?

Dainius Vaičiulis

It is fairly easy to close a file explorer based on its window name, like this:

msWinHelper Class:

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindow(string strClassName, string strWindowName);
    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

    static uint WM_CLOSE = 0x10;

    public IntPtr findWindow(string className, string name)
    {
        return FindWindow(className, name);
    }

    public void closeWindow(string className, string windowName)
    {
        SendMessage(findWindow(className, windowName), WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
    }

Main program:

msWinHelper.closeWindow(null, "WindowName");

However in some occasions this is not sufficient. Lets say I have two directories with the same subdirectories. If I open a subdir DIR1 from parent folder FOLD1 and another DIR1 from FOLD2, window names become the same. Therefore code cannot determine which is the parent folder and would not necessarily close the one I want.

Is it possible to close an opened folder based on its full path?

Alex K.

The shell has an automation interface that allows enumeration of explorer windows.

Add COM references to shell32.dll and shdocvw.dll then you can:

foreach (SHDocVw.InternetExplorer window in new SHDocVw.ShellWindows())
{
    if (Path.GetFileNameWithoutExtension(window.FullName).ToLowerInvariant() == "explorer")
    {
        if (Uri.IsWellFormedUriString(window.LocationURL, UriKind.Absolute))
        {
            string location = new Uri(window.LocationURL).LocalPath;

            if (string.Equals(location, @"C:\close\me", StringComparison.OrdinalIgnoreCase))
                window.Quit();
        }
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to close File Explorer in `selenium webdriver`

How to Open and Close Internet Explorer from batch file?

How to close file explorer window using lotus script

How to properly close a file in C

Close the file in vim without closing the file explorer

Is C# File.Copy close to the same thing as copying a file via explorer in terms of performance?

How to set the Explorer window of a specific file as a child window of TopMost form?

How do I write to a specific file/location with gawk close()

My C++ program is deleted when I close my file explorer

How to close Internet Explorer from the Run box?

VBA/ How to close Project Explorer window with a shortcut

Close file in C

C#:Regex How to Match specific div close tag but the last close tag?

How to navigate through directories using c++ to create a file explorer

How to search for C# source code files in Windows File Explorer?

How to make .i and .s file visible in a C project explorer?

How to close a file?

How to close enumerated file?

How to close a specific window with jQuery

How to close a specific folder with VBScript?

close all open folders in file explorer using batch

How can I get a button to open a specific folder in file explorer while building an Android app?

How to catch ctrl+c and close file descriptor in Perl

Close specific Excel file using Powershell

How to prevent file stacking in Explorer?

How to reveal current file in Explorer?

Close all Windows File Explorer windows, but don't kill the explorer.exe shell

C# VSTO Outlook 2013 - Explorer Close handle Save Event

How to update a specific value in a specific line of a text file c#