简体   繁体   中英

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.

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

You can try like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM