代码:
static void MultipleFilesToSingleFile(string dirPath, string filePattern, string destFile)
{
string[] fileAry = Directory.GetFiles(dirPath, filePattern);
Console.WriteLine("Total File Count : " + fileAry.Length);
using (TextWriter tw = new StreamWriter(destFile, true))
{
foreach (string filePath in fileAry)
{
using (TextReader tr = new StreamReader(filePath))
{
tw.WriteLine(tr.ReadToEnd());
tr.Close();
tr.Dispose();
}
Console.WriteLine("File Processed : " + filePath);
}
tw.Close();
tw.Dispose();
}
}
这个需要优化,因为速度非常慢:处理平均大小为40-50MB的45个XML文件需要3分钟。
请注意:45个平均为45MB的文件只是一个例子,可能有n个大小为m的文件,其中n在数千个范围内,m的平均值可以为128KB。简而言之,它可能会有所不同。
请问您对优化有任何看法吗?
using
块的情况下,调用Dispose
方法是多余的,因为你要处理的对象已经在using
块中(它将替你处理Dispose
)。 - Tim