维克斯复制文件; "ERROR 5: Access is denied" 即使 运行 管理员权限

wxCopyFile; "ERROR 5: Access is denied" even when running with administrator privilages

我正在编写一个非常简单的程序来编辑 XML 文件并将用户指定的图像复制到运行 .exe 的文件夹中。

我遇到的问题是 wxCopyFile。当我第一次 运行 程序(在 Visual Studio 2019 中使用调试模式)时,我收到“错误 5:访问被拒绝”,所以我尝试 运行 Debug 文件夹中的 .exe。同样,我遇到了同样的错误。然后我 运行 它作为管理员,我又得到了同样的错误!之后,我将.exe复制到另一个文件夹,用管理员权限试了一下,还是不行!

这是程序要求用户 select 他想要的图像的代码:

void GUI::OnSelectImg(wxCommandEvent& event) {
    wxFileDialog* selectImage = new wxFileDialog(NULL,
        wxString("Select the country's flag..."),
        wxEmptyString,
        wxEmptyString,
        wxT("PNG image files (*.png)|*.png") //Allow only pngs!
    );

    if (selectImage->ShowModal() == wxID_CANCEL) {
        delete selectImage;
        return;
    }

    fileMGR.SetImagePath(selectImage->GetPath());
    imagePathLabel->SetLabel(selectImage->GetPath()); //Updates the label
    delete selectImage;
}

wxCopyFile部分

void GUI::OnAddCountry(wxCommandEvent& event) {
    //Has he specified a flag?
    if (fileMGR.GetImagePath().IsEmpty()) {
        wxMessageBox(
            wxString("You have not specified an image for the country's flag!\nSpecify one and try again!"),
            "No image selected!",
            wxOK | wxICON_ERROR,
            this
        );
        return;
    }

    if (!wxCopyFile(fileMGR.GetImagePath(), wxStandardPaths::Get().GetDataDir())) {
        wxMessageBox(
            wxString("Failed to copy the selected image!"),
            "Failed to copy the image!",
            wxOK | wxICON_ERROR,
            this
        );
        return;
    }

    //Other not important actions...
}

这里还有the error window。我做错了什么?提前致谢!

wxCopyFile() documentation看来,它的两个参数都必须是文件名;但是,您的第二个参数是一个目录。这导致该函数尝试在已存在同名目录的情况下创建文件,从而导致错误。

因此,您也需要为第二个参数提供包含文件名的完整路径,而不仅仅是目录名。 wxCopyFile() 似乎模仿了它在 MSW 上使用的 CopyFile()。可以说他们的行为不是最方便的,但这超出了这个问题的重点。