How to get all driver letter in windows platform

I am consider how to do this.Should I use fllowing code try all letter,or is there a convenient api somewhere?

    let mut letter = vec!["A:\\", "B:\\", "C:\\", "D:\\",".....","Z:\\"];

    let mut exit_letter_vec = vec![];
    for x in letter {
        let path = Path::new(x);
        if path.exists() {
            exit_letter_vec.push(path);
        }
    }
    for x in exit_letter_vec {
        println!("{:?}", x.as_os_str());
    }

You should be able to use windows::Win32::Storage::FileSystem::GetLogicalDrives() from the windows crate. This returns a bitmap of all the available drives (MSDN docs).

There is also the windows::Win32::Storage::FileSystem::GetLogicalDriveStringsA() function (MSDN), but that requires you to pass in a suitably sized buffer and parse out the null-terminated strings that were written into it by Windows.

4 Likes

Thank you very much. :grinning: