3

I am currently using the following to Get Path in a ListBox of all Open Explorer Windows:

foreach (SHDocVw.InternetExplorer j in new SHDocVw.ShellWindows()) {
    if (j.Name == "Windows Explorer") {
        ListView1.Items.Add(j.LocationURL);
    }
}

But it doesn't return the location of special folders like Computer, Recycle Bin, Network etc. Is there a way to identify those folders so I can put the path on my own like shell:MyComputerFolder for Computer?

Community
  • 1
  • 1
Elmo
  • 6,409
  • 16
  • 72
  • 140
  • 2
    One problem that you may run into is that not all explorer windows are bound to path. For instance, in Windows 7 there are "Libraries" such as "Documents" and "Music". These libraries are collections of files and folders and aren't rooted at a file system point. – Chris Haas Jan 10 '12 at 22:51
  • Since, as others have pointed out, some "folders" don't *have* a path, perhaps if you explained what you were planning to *do* with these paths, we could make some suggestions. – Damien_The_Unbeliever Jan 11 '12 at 15:42
  • I just want to identify the open special folders and record them in my app – Elmo Jan 11 '12 at 20:22

4 Answers4

3

You need to use the Environment.GetFolderPath(Environment.SpecialFolder) method

Environment.SpecialFolder is an enum that has values for all Windows 'special' folders (e.g. My Documents, Program Files, Desktop)

Update: You can use this method to tell whether a given path is a special folder:

public static bool IsSpecialFolder(string folderPath)
{
    foreach (Environment.SpecialFolder specialFolderType in Enum.GetValues(typeof (Environment.SpecialFolder)))
    {
        var specialFolderLocation = Environment.GetFolderPath(specialFolderType);

        if(specialFolderLocation.Equals(folderPath, StringComparison.InvariantCultureIgnoreCase))
            return true;
    }

    return false;
}

For example, you could call IsSpecialFolder(j.LocationURL) for each j to find out which of the open folders are special folders.

Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
  • you could compare `j.LocationURL` to the locations of the Special Folders (determined as described above) to see if some of the open explorer windows point to a special folder. – Cristian Lupascu Jan 10 '12 at 21:26
  • @Programmer: for example `Environment.GetFolderPath(Environment.SpecialFolder.Desktop)` returns `C:\Users\\Desktop`, so yes, it's possible to get the path of special folders (Program Files, Windows, etc.) – Cristian Lupascu Jan 11 '12 at 20:23
  • Now I understand what the real problem is and I can't see a reliable solution for this. I've been thinking of using a combination of the `FullName` (the executable path) and `LocationName` (the caption) to identify such folders (e.g. when `ie.FullName==@"C:\Windows\Explorer.EXE" && ie.LocationName=="Computer"`), but localization will make this fail. – Cristian Lupascu Jan 11 '12 at 21:47
1

If j.LocationName was null I compared it with the current system folders' name : Get Current Names of Windows Special Folders, then added the path manually like shell:MyComputerFolder

Community
  • 1
  • 1
Elmo
  • 6,409
  • 16
  • 72
  • 140
0

Can use Environment.SpecialFolder enumeration like this

Environment.GetFolderPath(Environment.SpecialFolder.System))

In link, there is, by the way a concrete code example.

Tigran
  • 61,654
  • 8
  • 86
  • 123
0

I need to get the path ... But it doesn't return the location of special folders like Computer, Recycle Bin, Network etc.

The immediate problem you're running into is that LocationURL returns "" if the window's location is not a physical directory. For "My Computer", "Recycle Bin", and "Network", this isn't surprising -- those are virtual folders; there isn't a path or URL that could point to them, because they're not locations on disk.

However, if you navigate to "Documents" (which does correspond to a directory on disk, even though it's also a special folder), LocationURL still returns "". Here it could give you a path, but it chooses not to. This seems somewhat mean-spirited of it.

I found some documentation that says that ShellWindows returns InternetExplorer objects. I couldn't find any docs for the ShellBrowserWindow class you're using but InternetExplorer appears to be similar or identical, so it's got some documentation you can refer to instead of just having to look at the property names in Intellisense.

The only other property that looks useful is LocationName, which does return something even for virtual or special folders. However, it returns a string like "Documents" or "Libraries", which isn't something you could really make any use of programmatically (it'd be different in different locales, different Windows versions, etc.)

If all you need is something to show in a list, LocationName would probably suffice. If you actually need the path, you're probably out of luck (though you would be anyway, because as noted, things like Computer, Recycle Bin, and Network don't have paths).

Joe White
  • 94,807
  • 60
  • 220
  • 330
  • They do have paths like `shell:MyComputerFolder` – Elmo Jan 11 '12 at 16:47
  • @Programmer, that's a new one on me; interesting (and cool). But it looks like that's just a shortcut you can use in Windows Explorer; see e.g. [Shell Commands Windows 7](http://www.lancelhoff.com/shell-commands-windows-7/). You can't go to a command prompt and type "cd shell:MyComputerFolder", or write code to get a directory listing of it; it's just a UI-level convenience. The underlying OS simply doesn't have a textual name for things like Recycle Bin; it uses PIDLs to identify shell objects. And unfortunately, it looks like the InternetExplorer objects don't have a way to get the PIDL. – Joe White Jan 11 '12 at 17:19
  • Will `LocationName` work for you? It would be good enough to display in a list, but probably not good enough to identify them programmatically. – Joe White Jan 11 '12 at 17:30