2

I am wondering if I can close an explorer window which is communicating with my USB drive. I can get the removable disk and its drive letter by using

DriveInfo[] drives = DriveInfo.GetDrives();

foreach (DriveInfo drive in drives)
{
    if (!drive.IsReady)
    {
        continue;
    }

    if (drive.DriveType == DriveType.Removable && isDirectoryEmpty(drive.Name) == true)
    {
        //do stuff
    }
}

How do I do that ? any help would be appreciated.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Hybrid Developer
  • 2,320
  • 1
  • 34
  • 55
  • http://stackoverflow.com/questions/13501771/closing-a-specific-instance-of-windows-explorer might help. – Vladimir Apr 29 '16 at 10:02

2 Answers2

2

You cannot use Process.GetProcessesByName("explorer") because there is always one explorer process in the returned array, and by killing it, you would kill the window task bar too.

You have to use a COM library as explained here: https://stackoverflow.com/a/13464352/1280523

Matthieu Charbonnier
  • 2,794
  • 25
  • 33
0

You can try like this:

foreach (Process p in Process.GetProcessesByName("explorer"))
{
  if (p.MainWindowTitle.ToLower().Contains(@"yourSpecificWindow"))
  { 
    p.Kill();
  }
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • This cannot works. There is only one explorer process with an empty `MainWindowTitle`, even if you have several explorer windows.... And if you kill explorer, the window task bar is killed too.... See my answer – Matthieu Charbonnier Aug 07 '18 at 08:32
  • This works as someMain windowtitke are not empty. Try opening documents window. – Jin Thakur Feb 02 '22 at 13:45