My UWP app won't read a txt file [duplicate]

浪尽此生 提交于 2019-12-08 06:02:30

问题


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".


回答1:


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



来源:https://stackoverflow.com/questions/41557782/my-uwp-app-wont-read-a-txt-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!