问题
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?
回答1:
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.
回答2:
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
回答3:
Can use Environment.SpecialFolder enumeration like this
Environment.GetFolderPath(Environment.SpecialFolder.System))
In link, there is, by the way a concrete code example.
回答4:
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).
来源:https://stackoverflow.com/questions/8810606/identify-all-open-system-special-folders