3

A very belated improvement to the filtering of the Browse for Folder dialog so i...

 7 months ago
source link: https://devblogs.microsoft.com/oldnewthing/20231006-00/?p=108863
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A very belated improvement to the filtering of the Browse for Folder dialog so it shows only drive letters

RaymondChen_5in-150x150.jpg

Raymond Chen

October 6th, 20232 0

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 Path­IsRoot. While this successfully allows drives A:\ through Z:\, the Path­Is­Root function also reports \\a through \\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 \\a or \ 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))) &&
        /* PathIsRoot(buf) */
        PathGetDriveNumber(buf) >= 0) 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 Path­Is­Root to verify that you got a root, and then using Path­Get­Drive­Number to verify that you got a drive.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK