【问题标题】:wxCopyFile; "ERROR 5: Access is denied" even when running with administrator privilages蜡复制文件; “错误 5:访问被拒绝”即使以管理员权限运行
【发布时间】:2021-03-05 10:23:10
【问题描述】:

我正在编写一个非常简单的程序,它编辑 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。我做错了什么?提前致谢!

【问题讨论】:

  • 测试 - 你可以手动复制文件吗,无论是从命令提示符还是拖放?
  • 是的,我可以,无论是通过拖放还是使用 CMD(甚至没有授予它管理员权限)
  • 那么当你的程序运行时,目标文件可能正在使用(打开)。
  • @RichardCritten 目标文件和源文件均未打开。
  • 它可能在运行时被你的 .exe 锁定,正如@RichardCritten 所暗示的那样......一旦你打开文件并将其加载到你的程序中,检查它是否确实被锁定(在复制之前)跨度>

标签: c++ wxwidgets


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2016-01-18
    • 2018-03-27
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    相关资源
    最近更新 更多