File not found when trying to read UWP C++

1

I'm trying to get a string from text file.
I create my text file by Right click on project name -> Add -> New Item...
The file Properties is set like this Excluded from Build -> No, Content -> Yes
And this is the code for reading the file.

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);
    });
}

I followed this tutorial.


This is the error
Exception thrown at 0x773A1812 in WASAPI_testApp1.exe: Microsoft C++ exception: Platform::COMException ^ at memory location 0x0083E280. HRESULT:0x80070002 指定されたファイルが見つかりません。 WinRT information: 指定されたファイルが見つかりません。 occurred
Sorry for the Japanese 指定されたファイルが見つかりません。mean The specified file could not be found.

uwp
c++-cx
asked on Stack Overflow Nov 21, 2018 by peachy__kat • edited Nov 21, 2018 by peachy__kat

1 Answer

1

If the file is part of your project, it is not in ApplicationData::Current->LocalFolder, it is in Package::Current->InstalledLocation. Update your code to look there (after packaging it with your project) and it should work. Note that this location is read-only though; you can't write to your InstalledLocation. If you want to modify the file, you will need to copy it to LocalFolder first.

answered on Stack Overflow Nov 29, 2018 by Peter Torr - MSFT

User contributions licensed under CC BY-SA 3.0