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