AppendAllLines替代解决方案

本文关键字:解决方案 AppendAllLines | 更新日期: 2023-09-27 18:29:48

我正试图从C#编写一个txt文件,如下所示:

 File.WriteAllText("important.txt", Convert.ToString(c));
 File.AppendAllLines("important.txt", (from r in rec
                     select r.name + "   " + r.num1 + "   " + r.num2 + "   " + r.mult + "   " + r.rel).ToArray());

我得到错误AppendAllLInes找不到系统。IO.提交任何替代方法或如何包含AppendAllLInes

AppendAllLines替代解决方案

一次写入所有内容。

var part1 = Convert.ToString(c);
var part2 = String.Join(Environment.NewLine, 
                rec.Select(r => r.name + "   " + r.num1 + "   " + r.num2 + "   " + r.mult + "   " + r.rel)
               .ToArray());
System.IO.File.WriteAllText("important.txt", part1 + part2);

您也可以在3.5:中使用WriteAllLines

var allLines = new []{Convert.ToString(c)}
              .Concat(rec.Select(r => r.name + "   " + r.num1 + "   " + r.num2 + "   " + r.mult + "   " + r.rel))
              .ToArray();
System.IO.File.WriteAllLines("important.txt", allLines);