【问题标题】:use c++ to detect USB drive and copy files from USB to C:/使用 C++ 检测 USB 驱动器并将文件从 USB 复制到 C:/
【发布时间】:2023-01-07 04:47:14
【问题描述】:

尝试使用 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: %lu\n", 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 的计算机。我可以整天插入 USB 驱动器并右键单击、复制和粘贴文件夹,但是当我运行以下代码时,它什么也不做。此外,以下代码适用于没有 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();

所有这些都在没有 Symantec 的设备上运行,但我仍然可以在 Symantec 设备上手动复制。需要一种读取和验证 USB 的方法。

【问题讨论】:

  • 那么,您在什么时候尝试复制文件?
  • 在案例 DRIVE_REMOVABLE 之后,我尝试使用文件系统进行复制。如果我手动放置任何卷(D:、E: 等),它就可以复制,但无论它选择什么卷字母,我都需要这样做。我将在多台计算机上使用,因此每次可能会选择不同的音量。看起来我必须将“驱动器”变量作为字符串传递才能复制文件,但我尝试了几个实例但无法让它工作。
  • 你能告诉我们你在调用文件系统副本时的尝试(成功/手动和失败)吗?也许您需要删除/替换反斜杠字符或类似字符?
  • 添加了附加信息。还是没有变化。

标签: c++ copy usb


【解决方案1】:

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

#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: %lu
", GetLastError());
    }
    else
    {
        for (LPWSTR drive = myDrives; *drive != 0; drive += 4)
        {
            driveType = GetDriveTypeW(drive);
            if (driveType == DRIVE_REMOVABLE) {
                wprintf(L"
USB Drive detected in drive %s
", drive);
            }
           
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 2019-09-26
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 2023-04-07
    相关资源
    最近更新 更多