Reputation: 283
Letter of USB flash drive is changeable and I don't know how can I detect this or if there are more than one flash drive by using C++ or with a console line command(maybe a shortcut is exists like %APPDATA%).
How can I do it?
Upvotes: 0
Views: 2092
Reputation: 96
For this case you could use the GetDriveType
function:
UINT WINAPI GetDriveType(
_In_opt_ LPCTSTR lpRootPathName
);
Determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive.
This will suffice if the drive type makes no difference to you. If you are interested in listing only USB flash drives, consider checking the SetupDiGetDeviceRegistryProperty
function:
BOOL SetupDiGetDeviceRegistryProperty(
_In_ HDEVINFO DeviceInfoSet,
_In_ PSP_DEVINFO_DATA DeviceInfoData,
_In_ DWORD Property,
_Out_opt_ PDWORD PropertyRegDataType,
_Out_opt_ PBYTE PropertyBuffer,
_In_ DWORD PropertyBufferSize,
_Out_opt_ PDWORD RequiredSize
);
The SetupDiGetDeviceRegistryProperty function retrieves a specified Plug and Play device property.
Here's an example:
#include "stdafx.h"
#include <setupapi.h>
#include <devguid.h>
#include <cfgmgr32.h>
...
HDEVINFO hdevinfo = SetupDiGetClassDevs(&GUID_DEVCLASS_USB,
NULL, NULL, DIGCF_PRESENT);
if (hdevinfo == INVALID_HANDLE_VALUE)
return -1;
DWORD MemberIndex = 0;
SP_DEVINFO_DATA sp_devinfo_data;
ZeroMemory(&sp_devinfo_data, sizeof(sp_devinfo_data));
sp_devinfo_data.cbSize = sizeof(sp_devinfo_data);
while (SetupDiEnumDeviceInfo(hdevinfo, MemberIndex, &sp_devinfo_data))
{
DWORD PropertyRegDataType;
DWORD RequiredSize;
DWORD PropertyBuffer;
if (SetupDiGetDeviceRegistryProperty(hdevinfo,
&sp_devinfo_data,
SPDRP_CAPABILITIES,
&PropertyRegDataType,
(PBYTE) &PropertyBuffer,
sizeof(PropertyBuffer),
&RequiredSize))
{
if (PropertyBuffer & CM_DEVCAP_REMOVABLE)
{
// Do something, copy your files etc
}
}
MemberIndex++;
}
SetupDiDestroyDeviceInfoList(hdevinfo);
Upvotes: 2