簡體   English   中英

wx復制文件; “錯誤 5:訪問被拒絕”即使以管理員權限運行

[英]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...
}

這也是錯誤 window 我做錯了什么? 提前致謝!

wxCopyFile() 文檔看來,它的 arguments 都必須是文件名; 但是,您的第二個參數是一個目錄。 這導致 function 試圖創建一個已經存在同名目錄的文件,從而導致錯誤。

因此,您需要為第二個參數提供包含文件名的完整路徑,而不僅僅是目錄名。 wxCopyFile() 似乎模仿了它在 MSW 上使用的CopyFile() 可以說他們的行為不是最方便的,但這超出了這個問題的范圍。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM