How to read a text file in a universal Windows application

I am trying to read a text file called thedata.txt that has a list of words that I want to use in the executioner's game. I tried differently, but I cannot figure out where the file is placed, if at all when the application starts. I added the file to my project, and I tried to set the assembly properties to the content, and then to the embedded resource, but I can not find the file. I created a universal project for Windows 10. The code I tried looks like this:

Stream stream = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream("thedata.txt"); using (StreamReader inputStream = new StreamReader(stream)) { while (inputStream.Peek() >= 0) { Debug.WriteLine("the line is ", inputStream.ReadLine()); } } 

I get exceptions. I also tried to list the files in another directory:

  string path = Windows.Storage.ApplicationData.Current.LocalFolder.Path; Debug.WriteLine("The path is " + path); IReadOnlyCollection<StorageFile> files = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFilesAsync(); foreach (StorageFile file2 in files) { Debug.WriteLine("Name 2 is " + file2.Name + ", " + file2.DateCreated); } 

I donโ€™t see the file there either ... I want to avoid hard-coding the list of names in my program. I am not sure which path is in the file.

+5
source share
3 answers

the code is very simple, you just need to use a valid schema URI (ms-appx in your case) and convert your WinRT InputStream as a classic .NET stream:

 var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///thedata.txt")); using (var inputStream = await file.OpenReadAsync()) using (var classicStream = inputStream.AsStreamForRead()) using (var streamReader = new StreamReader(classicStream)) { while (streamReader.Peek() >= 0) { Debug.WriteLine(string.Format("the line is {0}", streamReader.ReadLine())); } } 

The properties of the built-in Build Action file must be set to Content, and Copy to Ouput Directory must be set to Do not Copy.

+17
source

You cannot use the classic .NET IO methods in Runtime Windows applications, the correct way to read a text file in UWP is:

 var file = await ApplicationData.Current.LocalFolder.GetFileAsync("data.txt"); var lines = await FileIO.ReadLinesAsync(file); 

In addition, you do not need a physical folder path - from msdn :

Do not use this property to access a folder because the file system path is not available for some folders. For example, in the following cases, the folder may not have a path to the file system or the file system path may not be available. โ€ข A folder is a container for a group of files (for example, the return value from some overloads of the GetFoldersAsync method) instead of the actual folder in the system file. โ€ข Folder supported by URI. โ€ข The folder was selected using the file collector.

+5
source

See File permissions for more information. And the Create, Write and Read file contains examples related to File IO for UWP applications in Windows 10.

You can get the file directly from the local application folder using the application URI, for example:

 using Windows.Storage; StorageFile file = await StorageFile.GetFileFromApplicationUriAsync("ms-appdata:///local/file.txt"); 
+3
source

Source: https://habr.com/ru/post/1239752/


All Articles