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.

7
  • 2
    Could you add your error message into your question?
    – Jesse
    Nov 21, 2018 at 8:02
  • So is sample.txt actually stored in ApplicationData::Current->LocalFolder? Also note that C++/CX is somewhat deprecated and C++/WinRT should be a preferred alternative now Nov 21, 2018 at 8:09
  • @VTT I'm sorry this is new to me. How do you store the file in LocalFolder? I just place it under project root folder (Same as .vcxproj) and Link it by set the Content property to Yes. Nov 21, 2018 at 8:24
  • I think marking item as content does nothing in case of C++ projects. So you probably need to copy this file to the desired location as a Post-Build step. Also if this file is supposed to be a part of the project then it will make sense to copy it into application folder (along with executable) instead of application data folder. Nov 21, 2018 at 8:39
  • 1
    You can add any assets you like to an application package distributed through the Microsoft Store. You just need to make sure that the assets are indeed added to the package. The choice of programming language doesn't make a difference. Nov 22, 2018 at 16:22

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.