Trey Balut
Trey Balut

Reputation: 1395

UWP write List<T> to a File

I'm trying to write a file in an UWP app that includes the items in a List of objects.

I write the file, but it only includes the first line.

Here is my code:

List<OPSDATA> SortedList = origList.OrderBy(o => o.OPS).ToList();

StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.CreateFileAsync("data.txt");

if (file != null)
{

    foreach (var item in SortedList)
    {
        await FileIO.WriteTextAsync(file, string.Format("{0},{1},{2}", item.OPS, item.LEAGUE, item.RPG));
    }
}

And here is the result:

0.858,0,5.4

SortedList has 60 elements.

`

Upvotes: 0

Views: 228

Answers (1)

Parrish Husband
Parrish Husband

Reputation: 3178

Why not use FileIO.WriteLinesAsync?

var sortedLines = origList.OrderBy(o => o.OPS)
                          .Select(i => $"{i.OPS},{i.LEAGUE},{i.RPG}");

StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.CreateFileAsync("data.txt");

if (file != null)
{
    await FileIO.WriteLinesAsync(file, sortedLines);
}

Upvotes: 3

Related Questions