仅知道驱动器号即可确定驱动器是否可移动(闪存或HDD) [英] determine if drive is removable (flash or HDD) knowing only the drive letter

查看:125
本文介绍了仅知道驱动器号即可确定驱动器是否可移动(闪存或HDD)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定文件是否在本地驱动器上.我发现 GetDriveType()WINAPI 检索驱动器类型.但是,阅读返回值的描述似乎是这样,而这也是我的理解方式,它会以FIXED形式检索闪存驱动器,这不是我想要的.

I am trying to determine if a file is on a local drive. I found GetDriveType() WINAPI which retrieves the drive type. However reading the description of the return values it seems, and thats how I understand it, that it retrieves a flash drive as FIXED which is not what I want.

它在本地驱动器上正常工作:

Its working fine on local drives:

bool IsDriveRemovableOrRemote(CString driveRoot)
{
    UINT driveType = GetDriveType(driveRoot);
    return (DRIVE_REMOVABLE == driveType || DRIVE_CDROM == driveType || DRIVE_NO_ROOT_DIR == driveType || DRIVE_REMOTE == driveType);
}

我没有用于测试ATM的闪存/外部驱动器,但是我想知道是否有人可以告诉我我的解释是否正确吗?如果是这样,我应该使用哪种更好的替代方法?

I don't have a flash/external drive to test ATM but I would like if someone can tell me if my interpretation is correct? and if so, what better alternative should I use?

请记住,我只有文件的路径.

Please bear in mind that I only have the path of the file.

推荐答案

您应该仔细阅读doco.虽然闪存驱动器被视为固定设备,但该链接页面上有一条注释:

You should read the doco more closely. While a Flash drive is considered a fixed device, there's a note in that linked page:

要确定驱动器是否为USB类型的驱动器,请调用SetupDiGetDeviceRegistryProperty并指定SPDRP_REMOVAL_POLICY属性.

To determine whether a drive is a USB-type drive, call SetupDiGetDeviceRegistryProperty and specify the SPDRP_REMOVAL_POLICY property.

如果仅从路径入手,则该过程似乎有些混乱,但是您可以开始阅读文档.

The process seems a little messy if all you start with is the path but you can start reading the doco here. It looks like you may need to enumerate the devices until you find one matching your drive.

为避免对您的所有请求执行此操作,我将分两个阶段进行检查.如果您当前的方法说它不是固定的,则将其视为非本地方法.

To avoid doing this to all your requests, I would do a two-stage check. If your current method says it's not fixed, treat it as non-local.

如果它说是固定的,那么可以肯定地使用我建议的方法枚举设备.

If it says it is fixed, then you can enumerate the devices using my suggested method to be certain.

或者,您可以在第一次需要时枚举所有固定的非USB驱动器,然后仅缓存信息.我非常确定这些驱动器的列表在系统运行时不会更改-根据定义,添加和删除的驱动器是可移动的.

Alternatively, you can enumerate all fixed non-USB drives the first time you need to, and then just cache the information. I'm pretty certain that the list of these drives won't change while the system is running - drives that get added and deleted are, by definition, removable.

这篇关于仅知道驱动器号即可确定驱动器是否可移动(闪存或HDD)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆