0

Thanks to the below link, I've found out how to load a TXT file into my Windows Phone 8.1 app. My question is specifically tied to WHY my code doesn't work. (My code begins after link to the other StackOverflow question).

This is the same question, with a working answer. Read text file in project folder in Windows Phone 8.1 Runtime

I set the StorageFile as the relative location of the file on the phone. I then try and use that location to open a StreamReader. This small snippet compiles fine, however on execution, encounters a runtime error, and "firstFile" is empty/null.

const string filename = "FileToRead.txt";

StorageFile firstFile = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);

using (StreamReader streamFirstFile = new StreamReader(await firstFile.OpenStreamForReadAsync())) 
        { 
        loadText_Button.Text = await streamFirstFile.ReadToEndAsync();
        }

To hopefully further clarify this specific question, are the following two lines of code the same, and if not, how am I using it wrong?

ApplicationData.Current.LocalFolder.GetFileAsync(fileName);

StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///Assets/FileToRead.txt"))

I even tried to set it as follows:

ApplicationData.Current.LocalFolder.GetFileAsync(String.Format("{0}{1}", "Assets/", fileName);

Thank you all in advance.

Community
  • 1
  • 1
RAB
  • 317
  • 3
  • 12
  • Where is the file saved to? Is it deployed as part of your XAP/APPX? – Rowland Shaw Feb 06 '15 at 17:20
  • are you sure that the file that you are looking for exist in the ApplicationData.Current.LocalFolder `const string filename = "FileToRead.txt";` if not then why not store the path in a .config file and make sure that you append the filename to the end of the path.. – MethodMan Feb 06 '15 at 17:23
  • The "FileToRead.txt" is located inside the Assets folder. Is that what you're asking? – RAB Feb 06 '15 at 17:26
  • Check your file's *Build Action*. Also [this answer](http://stackoverflow.com/a/24339587/2681948) may help. – Romasz Feb 06 '15 at 17:29
  • The link you provided, Romasz, was fantastic, and exactly what I was looking for. Thank you very much! Been trying to work through this for 2 days, without knowing where exactly to start! (I'd accept it as an answer, if it were posted as such...) – RAB Feb 06 '15 at 17:38

2 Answers2

3

If you want to read a file from your package then you should refer to Package.Current.InstalledLocation, not LocalFolder. You should also check if your resource has Build Action set to Content.

You can access your file in two ways - either getting it from InstalledLocation folder:

StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Data\" + fileName);

or by Uri:

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///Data/"+fileName));

Few notes:

  • files in package are read-only,
  • you can also access files in shared projects by providing its name before folder name
  • watch out for Build Action - some files are by default set as Content by VS and some not.

You can also find some useful information at this post.

Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154
0

A note for the unwary.

If the name of the file you placed in your Assets folder ends with ".json", the file won't be accessible from your code using the above given function calls. Simply rename the file so that it ends with ".txt".

I would have made this a comment but I don't have the required reputation.

Efe Ariaroo
  • 1,052
  • 10
  • 10