A very belated improvement to the filtering of the Browse for Folder dialog so it shows only drive letters
Quite some time ago, I showed how to filter the Browse for Folder dialog so it shows only drive letters. There’s a small quirk in the code, which it has taken me several years to getting around to addressing.
But hey, better late than never.
To recap, we detected drive letters by verifying that the length is at most 4, and that it is reported as a root directory by
PathIsRoot. While this successfully allows drives
PathIsRoot function also reports
\\z as roots if you had a server with a one-character name. It also reports
\ as a root, which is I guess sort of true?
Now, these aren’t problems in practice because you cannot add
\ as children of My Computer, or at least, you can’t do it today. But just to make sure, let’s reject those cases.
Fortunately, the fix is simple: Instead of using
PathIsRoot, we switch to
PathGetDriveNumber, which returns −1 if the path does not begin with a letter and a colon.
if (SUCCEEDED(StrRetToBuf(&str, pidlItem, buf, ARRAYSIZE(buf))) &&) return S_OK;
There is still a tiny hole here: If someday it becomes possible to add raw drive letters to My Computer, then this will accept
A: as well as
A:\. I’m not sure what that would even mean, but you could wear a belt and suspenders by using
PathIsRoot to verify that you got a root, and then using
PathGetDriveNumber to verify that you got a drive.