简体   繁体   English

UWP写清单 <T> 到文件

[英]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. 我正在尝试在UWP应用程序中编写一个文件,其中包含对象列表中的项目。

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 0.858,0,5.4

SortedList has 60 elements. SortedList有60个元素。

` `

Why not use FileIO.WriteLinesAsync ? 为什么不使用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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM