Search code examples
listuwptext-fileswindowsiot

C# UWP textfile to list of strings


I'm creating a UWP application (latest windows IoT) for my raspberry.

I'm trying to get a list of strings from a .txtfile located in the project folder under a map called Words.

This is my code so far.

public async void GenereerGokWoord(int character)
{
    StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(
                               new Uri("ms-appx:///Words//5-letterwoorden.txt")
                             );
    }

By setting a breakpoint at the end of the } I can confirm that this code can find the .txt file.

But now I don't know how to get a list of strings from this point on.

The .txt file looks like things

word 1

word 2

word 3


Solution

  • If you want contents of file as a List with each line as an Item, Use FileIO.ReadLinesAsync()

    IList<string> data = await FileIO.ReadLinesAsync(file);
    List<string> finallist = data.ToList();
    

    Your finallist should contain all words as List.