locked
GetFileFromApplicationUriAsync prolbem . How to get a txt file's content?

    Question

  •  Windows::Storage::StorageFile^ file;
     Windows::Foundation::Uri^ path = ref new Windows::Foundation::Uri(L"ms-appx:///Assets/Note.txt");
      file->GetFileFromApplicationUriAsync(path); 

    //here , file is nullptr!!!  why?  I make variable file as a class's member , and use file in another method delay some minites , but file  still be 0x000000

      if (file != nullptr)
            {
                create_task(FileIO::ReadTextAsync(file)).then([this, file](task<String^> task)
               {
                  try
                  {
                    String^ fileContent = task.get();
                   // OutputTextBlock->Text = "The following text was read from '" + file->Name + "':\n\n" + fileContent;
                  }
                  catch(COMException^ ex)
                  {
                   // rootPage->HandleFileNotFoundException(ex);
                  }
               });
           }



    Monday, May 06, 2013 4:15 AM

Answers

  • Try this:

    	auto path = ref new Uri("ms-appx:///Assets/Note.txt");
    	create_task(StorageFile::GetFileFromApplicationUriAsync(path)).then([this](task<StorageFile^> task)
    	{
    		try
    		{
    			auto file = task.get();		
    			create_task(FileIO::ReadTextAsync(file)).then([this, file](String^ fileContent)
    			{
    				OutputTextBlock->Text = "The following text was read from '" + file->Name + "':\n\n" + fileContent;				
    			});
    		}
    		catch (Exception^ e)
    		{
    			OutputTextBlock->Text = e->ToString();
    		}
    	});



    • Proposed as answer by jrboddie Monday, May 06, 2013 3:17 PM
    • Edited by jrboddie Monday, May 06, 2013 3:43 PM Corrected error handling
    • Marked as answer by Jesse Jiang Monday, May 13, 2013 6:31 AM
    Monday, May 06, 2013 2:07 PM