AppendAllLines替代解决方案

时间:2012-03-31 23:52:47

标签: c#

我正在尝试从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());

我收到system.IO.File找不到错误AppendAllLInes任何替代方法或如何包含AppendAllLInes

1 个答案:

答案 0 :(得分:0)

一次写下所有内容。

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);