C++ 无法在c+;中打开文件+;(directx应用程序)访问被拒绝

C++ 无法在c+;中打开文件+;(directx应用程序)访问被拒绝,c++,file,load,directx-11,dds-format,C++,File,Load,Directx 11,Dds Format,我试图用我的DirectX 11项目打开一个DDS文件,但是,在大多数情况下,它拒绝打开它。每次失败时,我都会收到E_ACCESSDENIED错误。 使其工作的唯一方法是将相对路径放置到当前目录或子目录。如果是父目录的相对路径,或者是绝对路径,则函数将失败 问题是我希望使用FileOpenPicker打开图像,所以在任何情况下,我都会得到一个绝对路径 我将分享我的职能: void Element::FileOpenDialog() { FileOpenPicker^ fileOpenPi

我试图用我的DirectX 11项目打开一个DDS文件,但是,在大多数情况下,它拒绝打开它。每次失败时,我都会收到E_ACCESSDENIED错误。 使其工作的唯一方法是将相对路径放置到当前目录或子目录。如果是父目录的相对路径,或者是绝对路径,则函数将失败

问题是我希望使用FileOpenPicker打开图像,所以在任何情况下,我都会得到一个绝对路径

我将分享我的职能:

void Element::FileOpenDialog()
{
    FileOpenPicker^ fileOpenPicker = ref new FileOpenPicker();
    fileOpenPicker->ViewMode = PickerViewMode::Thumbnail;
    fileOpenPicker->SuggestedStartLocation = PickerLocationId::PicturesLibrary;
    fileOpenPicker->CommitButtonText = "Load";
    fileOpenPicker->FileTypeFilter->Append(".dds");
    create_task(fileOpenPicker->PickSingleFileAsync()).then([this](StorageFile^ file)
    {
        if (file)
        {
            m_fullPath = const_cast<wchar_t*>(file->Path->Data());
            wcout << m_fullPath  << endl; // prints the correct path of the selected file

            m_loadedImage = false;
        }
        m_choseImage = true; // Checking in another code if the user chose an image to load.
    });
}
void元素::FileOpenDialog()
{
FileOpenPicker^FileOpenPicker=ref new FileOpenPicker();
fileOpenPicker->ViewMode=PickerViewMode::缩略图;
fileOpenPicker->SuggestedStartLocation=PickerLocationId::PicturesLibrary;
fileOpenPicker->CommitButtonText=“加载”;
fileOpenPicker->FileTypeFilter->Append(“.dds”);
创建任务(fileOpenPicker->PickSingleFileAsync())。然后([this](存储文件^file)
{
如果(文件)
{
m_fullPath=const_cast(文件->路径->数据());
wcout dev.Get(),L“texture\\texture.dds”,resource.GetAddressOf(),m_texture.release和GetAddressOf())返回false;//有效
如果(!FH::ThrowIfFailed(CreateDDSSTextureFromFile(m_gameWindow->dev.Get(),L.“.\\texture.dds”,resource.GetAddressOf(),m_texture.ReleaseAndGetAddressOf()))返回false;//E_ACCESSDENIED
如果(!FH::ThrowIfFailed(CreateDDSSTextureFromFile(m_gameWindow->dev.Get(),path,resource.GetAddressOf(),m_texture.ReleaseAndGetAddressOf())返回false;//E_ACCESSDENIED
返回true;
}
因为我不知道为什么,所以我来这里请求你的帮助


非常感谢!

UWP应用程序无法直接访问文件选择器选择的位置。
FileOpenPicker
是一个代理,它代表您执行此操作,但您不能使用标准文件I/O,只能在其上使用WinRT API。请记住,拾取的文件甚至可能不在本地文件系统上。仅您有直接I/O访问权的y文件位置是您的已安装文件夹(只读)、临时文件夹(读写)和应用程序数据文件夹(读写)

有关更多信息,请参阅MSDN上的

解决方案是将选定的代理文件复制到您确实有权访问的临时文件位置,然后在临时副本上使用
createddstructurefromfile

#include <ppltasks.h>
using namespace concurrency;

using Windows::Storage;
using Windows::Storage::Pickers;

create_task(openPicker->PickSingleFileAsync()).then([](StorageFile^ file)
{
    if (file)
    {
        auto tempFolder = Windows::Storage::ApplicationData::Current->TemporaryFolder;
        create_task(file->CopyAsync(tempFolder, file->Name, NameCollisionOption::GenerateUniqueName)).then([](StorageFile^ tempFile)
        {
            if (tempFile)
            {
                HRESULT hr = CreateDDSTextureFromFile(..., tempFile->Path->Data(), ...);
                DeleteFile(tempFile->Path->Data());
                DX::ThrowIfFailed(hr);
            }
        });
    });
#包括
使用名称空间并发;
使用Windows::Storage;
使用Windows::Storage::Pickers;
创建任务(openPicker->PickSingleFileAsync())。然后([](存储文件^file)
{
如果(文件)
{
auto tempFolder=Windows::Storage::ApplicationData::Current->Temporary文件夹;
创建任务(文件->复制异步(tempFolder,文件->名称,名称冲突选项::GenerateUniqueName))。然后([](存储文件^tempFile)
{
如果(临时文件)
{
HRESULT hr=CreateDDSSTextureFromFile(…,tempFile->Path->Data(),…);
删除文件(tempFile->Path->Data());
DX::ThrowIfFailed(hr);
}
});
});

上详细介绍了这一点,同时也介绍了书写案例。

非常感谢,我以前不知道,现在它可以工作了!
#include <ppltasks.h>
using namespace concurrency;

using Windows::Storage;
using Windows::Storage::Pickers;

create_task(openPicker->PickSingleFileAsync()).then([](StorageFile^ file)
{
    if (file)
    {
        auto tempFolder = Windows::Storage::ApplicationData::Current->TemporaryFolder;
        create_task(file->CopyAsync(tempFolder, file->Name, NameCollisionOption::GenerateUniqueName)).then([](StorageFile^ tempFile)
        {
            if (tempFile)
            {
                HRESULT hr = CreateDDSTextureFromFile(..., tempFile->Path->Data(), ...);
                DeleteFile(tempFile->Path->Data());
                DX::ThrowIfFailed(hr);
            }
        });
    });