wxCopyFile;即使以管理员权限运行,也会出现“错误 5:访问被拒绝”

时间:2021-03-05 10:23:10

标签: c++ wxwidgets

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

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

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

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。我做错了什么?提前致谢!

1 个答案:

答案 0 :(得分:2)

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

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