是否有与4.0的AppendAllLines等效的.NET 3.5? [英] Is there a .NET 3.5 equivalent of 4.0's AppendAllLines?
问题描述
var output = DataList.Select(x => x.blah blah blah);
string outputFilePath = @"C:\output.txt";
File.AppendAllLines(outputFilePath, output);
我上面的.NET 4代码被放置在for/next循环中,该循环重复了几次,因此附加是必需的(与覆盖/重写相反).现在,有人告诉我我必须改用.NET 3.5,并且AppendAllLines吐出错误"System.IO.File"不包含AppendAllLines的定义.
My above .NET 4 code was put in a for/next loop that iterates a few times, so appending is necessary (as opposed to overwriting/rewriting). Now, I've been told I have to use .NET 3.5 instead and AppendAllLines spits out the error 'System.IO.File' does not contain a definition for AppendAllLines.
有人知道不需要我彻底修改现有代码并使用.NET 3.5的修复程序吗?
Does anyone know of a fix that does not require me to change my existing code too drastically and uses .NET 3.5?
推荐答案
使用适当的流传输,框架中没有任何东西可以做到这一点,但是您可以很容易地编写自己的方法:
Nothing in the framework exists to do exactly that, using the proper streaming, but you can write your own method easily enough:
public static void AppendAllLines(string path, IEnumerable<string> lines)
{
using (var writer = new StreamWriter(path, true))
foreach (var line in lines)
writer.WriteLine(line);
}
这篇关于是否有与4.0的AppendAllLines等效的.NET 3.5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!