【发布时间】: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 所暗示的那样......一旦你打开文件并将其加载到你的程序中,检查它是否确实被锁定(在复制之前)跨度>