locked
Reading a file from Documens Library

    Question

  • I am trying to get a file from the MyDocuments folder in win8 in a c++ Metro style app by the below code:

    task<StorageFile^>(KnownFolders::DocumentsLibrary->GetFileAsync("MyFile.xml")).then([this](task<StorageFile^> getFileTask)

    {

    try

    {

    m_XmlFile = getFileTask.get();

    }

    catch (Platform::Exception^ e)

    {

    String^ Exce = e->ToString();

    }

    });

     

    But the code crashes. I have added the Documents folder capability to the app. The file is sure in the folder. It is not getting into the exception section. m_XmlFile is a valid StorageFile variable. What could be wrong? It is not getting into the try section..

    Thursday, May 24, 2012 2:53 PM

Answers

  • You declare the file association in the manifest. See the Manifest Designer docs.

    Applications can only natively see files in their own app data or installed location. Via capabilities they can receive brokered access to other locations such as the libraries. Via the file picker they can receive brokered access to any file the user chooses.

    Your app should always be able to access files in its app local folder. If you are having trouble with that I would recommend opening a thread specifically on that subject with exact details of how you are trying to open the file and how it is failing. If you can't open it in the browser then there is probably something going on at the file system level, but perhaps some other app has it opened exclusively.

    --Rob

    • Proposed as answer by Jesse Jiang Friday, May 25, 2012 7:00 AM
    • Marked as answer by Jesse Jiang Friday, June 01, 2012 8:22 AM
    Thursday, May 24, 2012 8:00 PM
    Owner

All replies

  • What error are you getting?

    My guess is that you are seeing an access denied on the .xml file. To open a file in the Documents folder you need both the Documents folder capability and a file association declaration that your app handles that document type.

    --Rob

    Thursday, May 24, 2012 3:19 PM
    Owner
  • Thank you Rob.

    1.How do I set a file association in my app?

    2. Also, cant metro style apps access files stored in locations other than MyDocs, MyPics etc and App local foder?

    3. I have also come across situations where I can access an xml file stored in  in App Local folder for sometime from my metro app. After a while the file gets corrupted and I am not able to even open it in my browser !! How come this happens?


    Thursday, May 24, 2012 6:03 PM
  • You declare the file association in the manifest. See the Manifest Designer docs.

    Applications can only natively see files in their own app data or installed location. Via capabilities they can receive brokered access to other locations such as the libraries. Via the file picker they can receive brokered access to any file the user chooses.

    Your app should always be able to access files in its app local folder. If you are having trouble with that I would recommend opening a thread specifically on that subject with exact details of how you are trying to open the file and how it is failing. If you can't open it in the browser then there is probably something going on at the file system level, but perhaps some other app has it opened exclusively.

    --Rob

    • Proposed as answer by Jesse Jiang Friday, May 25, 2012 7:00 AM
    • Marked as answer by Jesse Jiang Friday, June 01, 2012 8:22 AM
    Thursday, May 24, 2012 8:00 PM
    Owner