peachy__kat
peachy__kat

Reputation: 61

File not found when trying to read UWP C++

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.

Upvotes: 1

Views: 564

Answers (1)

Peter Torr
Peter Torr

Reputation: 12019

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.

Upvotes: 2

Related Questions