尝试读取UWP C ++时找不到文件

问题描述 投票:1回答:1

我正在尝试从文本文件中获取字符串。 我通过右键单击项目名称 - >添加 - >新项目来创建我的文本文件... 文件属性设置如下:从构建中排除 - >否,内容 - >是 这是用于读取文件的代码。

void MyApp::MainPage::btn_readFile_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^e)
{
    StorageFolder^ storageFolder = ApplicationData::Current->LocalFolder;
    create_task(storageFolder->GetFileAsync("sample.txt")).then([](StorageFile^ sampleFile)
    {
        return FileIO::ReadBufferAsync(sampleFile);
    }).then([](Streams::IBuffer^ buffer)
    {
        auto dataReader = DataReader::FromBuffer(buffer);
        String^ bufferText = dataReader->ReadString(buffer->Length);
    });
}

我跟着this tutorial

这是错误 在WASAPI_testApp1.exe中的0x773A1812抛出异常:Microsoft C ++异常:Platform :: COM异常^在内存位置0x0083E280。 WinRT信息:找不到指定的文件。发生 对不起日语无法找到指定的文件。 mean无法找到指定的文件。

uwp c++-cx
1个回答
1
投票

如果文件是你的项目的一部分,它不在ApplicationData::Current->LocalFolder,它在Package::Current->InstalledLocation。更新您的代码以查看(在将其与您的项目打包后),它应该可以工作。请注意,此位置是只读的;你不能写你的InstalledLocation。如果要修改文件,则需要先将其复制到LocalFolder

© www.soinside.com 2019 - 2024. All rights reserved.