1

I was wondering if anyone would know how to get an Universal Windows app to read a txt file.

StreamReader lezer = new StreamReader(@"C:\Users\LotteDiesveld\Documents\Visual Studio 2015\Projects\Jaar 1\Periode 2\OIS 12\Eindopdracht\Personen.txt");
tbPersonen.Items.Add(lezer.ReadToEnd());

I've also tried this

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync("ms-appx:///reserveringen.txt")

I know multiple answers have been given on this subject, but none work, because I keep getting an error. This error is either "Cannot convert type string to type uri" or "cannnot convert type string to type stream".

L.Dies
  • 7
  • 5
  • Your second method should work if the file's build type is set to `Content`, but you'll need to wrap your path in `new Uri(...path...)`. – shawnseanshaun Aug 19 '17 at 03:02

1 Answers1

3

A UWP app runs in a sandboxed environment and has no permissions to read a file from anywhere on the hard drive. If you want to read a file in the user's Documents Library you should either let the user select the file using a picker or declare the folder in the application's manifest. Please refer to Jerry Nixon's blog post for more information: http://blog.jerrynixon.com/2012/06/windows-8-how-to-read-files-in-winrt.html

Please also refer to the following similar question:

Windows 10 Universal App File/Directory Access

For information about how to read a file that is distributed along with your app, please see the answer here:

how to read a text file in windows universal app

Community
  • 1
  • 1
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thanks, I know it's been answered a couple of times, but the thing it I keep getting the same error with every solution provided by stackoverflow members. The error is either "can't convert type string to type stream" or in case I would use an Uri "can't convert type string to type uri". So now I just don't know how to solve the problem – L.Dies Jan 09 '17 at 22:32