使用c++检测USB驱动器并将文件从USB复制到c:/



尝试使用C++检测USB驱动器(此部分有效(,然后将文件从USB驱动器复制到C:。我尝试过使用几种不同的变体将变量传递到字符串,但没有成功。使用以前响应中的回收代码,但在检测到DRIVE_REMOVABLE后仍然无法将文件从USB复制到C:。有人有什么想法吗?

#include<iostream>
#include<tchar.h>
#include<Windows.h>
using namespace std;
int main() {
    WCHAR myDrives[105];
    WCHAR volumeName[MAX_PATH];
    WCHAR fileSystemName[MAX_PATH];
    DWORD serialNumber, maxComponentLen, fileSystemFlags;
    UINT driveType;
    if (!GetLogicalDriveStringsW(ARRAYSIZE(myDrives) - 1, myDrives))
    {
        wprintf(L"GetLogicalDrives() failed with error code: %lun", GetLastError());
    }
    else
    {
        wprintf(L"This machine has the following logical drives:n");
        for (LPWSTR drive = myDrives; *drive != 0; drive += 4)
        {
            driveType = GetDriveTypeW(drive);
            wprintf(L"Drive %s is type %d - ", drive, driveType);
            switch (driveType)
            {
            case DRIVE_UNKNOWN:
                wprintf(L"Cannot be determined!");
                break;
            case DRIVE_NO_ROOT_DIR:
                wprintf(L"Invalid root path/Not available.");
                break;
            case DRIVE_REMOVABLE:
                wprintf(L"Removable.");
                break;
            case DRIVE_FIXED:
                wprintf(L"Fixed.");
                break;
            case DRIVE_REMOTE:
                wprintf(L"Network.");
                break;
            default:
                wprintf(L"Unknown value!");
            }
        }
    }

经过进一步的调查,我意识到问题不在我的代码本身,而是在设备本身。此问题涉及使用Symantec Endpoint Protection的计算机。我可以整天插入U盘并右键单击、复制和粘贴文件夹,但当我运行以下代码时,它什么都不做。此外,以下代码适用于没有Symantec Endpoint的设备。我试过这个:

fs::path source5 = "F:\Install files";
            fs::path targetParent5 = "C:/";
            auto target5 = targetParent5 / source5.filename(); 
            try
            {
                fs::create_directories(target5); 
                fs::copy(source5, target5, fs::copy_options::recursive);
            }
            catch (std::exception& e) 
            {
                std::cout << e.what();
            }

并尝试过:

std::ifstream src("F:\Install files", std::ios::binary);
            std::ofstream dst("C:\", std::ios::binary);
            dst << src.rdbuf();

所有人都在没有赛门铁克的设备上工作过,但我仍然可以在赛门铁科设备上手动复制。需要一种读取和验证USB的方法。

问题是组策略。一旦解决了这个问题,解决方案现在就起作用了。以下是对我有效的:

#include<iostream>
#include<tchar.h>
#include<Windows.h>
using namespace std;
int main() {
    WCHAR myDrives[105];
    WCHAR volumeName[MAX_PATH];
    WCHAR fileSystemName[MAX_PATH];
    DWORD serialNumber, maxComponentLen, fileSystemFlags;
    UINT driveType;
    if (!GetLogicalDriveStringsW(ARRAYSIZE(myDrives) - 1, myDrives))
    {
        wprintf(L"GetLogicalDrives() failed with error code: %lun", GetLastError());
    }
    else
    {
        for (LPWSTR drive = myDrives; *drive != 0; drive += 4)
        {
            driveType = GetDriveTypeW(drive);
            if (driveType == DRIVE_REMOVABLE) {
                wprintf(L"nUSB Drive detected in drive %sn", drive);
            }
           
        }
    }
}

最新更新