Hybrid Developer
Hybrid Developer

Reputation: 2340

How to close a particular windows explorer window in c#?

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.

Upvotes: 2

Views: 3393

Answers (2)

Matthieu Charbonnier
Matthieu Charbonnier

Reputation: 2982

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

Upvotes: 2

Rahul Tripathi
Rahul Tripathi

Reputation: 172398

You can try like this:

foreach (Process p in Process.GetProcessesByName("explorer"))
{
  if (p.MainWindowTitle.ToLower().Contains(@"yourSpecificWindow"))
  { 
    p.Kill();
  }
}

Upvotes: 0

Related Questions