这是我的代码
class Program
{
static void Main(string[] args)
{
(from d in new DirectoryInfo("E:\\")
.GetDirectories() select d.Name)
.WriteToConsole(Logger.WriteToLog , true);
// .WriteToConsole(Console.WriteLine);
}
}
public static class Logger
{
public static void WriteToLog(dynamic data)
{
string filePath = string.Format("{0}\\log.txt", Environment.CurrentDirectory);
var stream = File.Create(filePath);
stream.Write(Encoding.UTF8.GetBytes(data), 0, Encoding.UTF8.GetByteCount(data));
stream.Flush();
stream.Close();
}
}
public static class WriteExtensions
{
public static void WriteToConsole<T1>(this IEnumerable<T1> data , Action<T1> action, bool carelist = false)
{
if (carelist)
{
// all i want is to invoke action by sending data rather than
// one by one
return;
}
foreach (var item in data)
{
action.Invoke(item);
}
}
}
问题是我们可以逐行写入控制台..但是对于写入文件,我想一次全部写入..任何帮助..谢谢