locked
Accessing a text file from a specific directory or app folder C++

    Question

  • Hello there,

    I'm creating a basic 2D metro app based game and I want file access to a specific path (not decided yet) so that I can read a text file and use it in my game. For example I may want to read a text file that lays out all the tiles in my level.

    At the moment I have been able to achieve some sort of file reading and writing but only in the documents library. Here is some of the code from a class I created:

    m_documents = KnownFolders::DocumentsLibrary;

    String^ fileName = L"test.txt";

    create_task(m_documents->GetFileAsync(fileName)).then([this] (StorageFile^ returnedFile) { m_file = returnedFile; }); create_task(FileIO::ReadTextAsync(m_file)).then([this] (String^ contents) { m_contents = contents; });


    Basically the text file at the moment just says "Hello World" and this can be seen after the file is read from the documents library but I don't want to put game specific text in the documents library (That would also mean copying it when the game is first launched, not something the player needs to see). I've tried using this:

    create_task(PathIO::ReadTextAsyc(L"c:\test.txt"));

    But I get an invalid parameter exception. If anyone can shed light on the issue or suggest a better way of doing things I'd be all ears.

    Thanks,

    Vince

    Monday, August 27, 2012 2:53 PM

Answers

  • You can package the text file with the app by including it in your project and marking it as Content. It will be included in the appx package and installed with the application, where it can be accessed with the ms-appx: protocol or via the Package.InstalledLocation StorageFolder.

    If you don't want it packaged but want to generate it at first run then you would need write access. In this case, putting it in ApplicationData.LocalFolder would be more appropriate.

    --Rob

    • Marked as answer by Vince0656 Tuesday, August 28, 2012 9:07 AM
    Monday, August 27, 2012 9:03 PM
    Owner

All replies

  • Hi Vince,

    See Managing application data.

    You should store the files in either your install directory (if you don't need to write them: this is a likely place for static level information) or to your application data directory (if you do need to write to them: a good place for user edited levels). The documents directory is for user document files which the user should know about and be expected to touch, move, delete, etc. (appropriate if the user explicitly manages level documents).

    Apps don't have access to most of the file system: they can't see the c:\ directory. Apps have direct access only to their install directory and application data directories

    Your app can get brokered access to other directories (e.g. libraries declared with capabilities, user selected folders via the FolderPicker) through StorageFile and StorageFolder objects. When you use these, the broker process does the actual file access on your behalf. The data within the file is provided to the app via a stream returned from StorageFile::OpenAsync.

    See Accessing data and files for more information.

    --Rob

    Monday, August 27, 2012 3:35 PM
    Owner
  • Thanks for that Rob, very informative. Seeing as it will be read only I think the install directory sounds good but how would I place it there after first run? I need to package this text file with the app if possible.

    It may have this in it:

    456

    569

    This would need to be read every time the game starts to place the tiles but allows much more flexibility when designing the level in the first place.

    Monday, August 27, 2012 4:03 PM
  • You can package the text file with the app by including it in your project and marking it as Content. It will be included in the appx package and installed with the application, where it can be accessed with the ms-appx: protocol or via the Package.InstalledLocation StorageFolder.

    If you don't want it packaged but want to generate it at first run then you would need write access. In this case, putting it in ApplicationData.LocalFolder would be more appropriate.

    --Rob

    • Marked as answer by Vince0656 Tuesday, August 28, 2012 9:07 AM
    Monday, August 27, 2012 9:03 PM
    Owner
  • Thank you so much Rob. I'm using "ms-appx://test.txt" and it is working flawlessly in conjunction with PathIO so massive thanks for helping me.
    Tuesday, August 28, 2012 9:07 AM