我先用StringBuilder写下了下面的代码,然后打开Out Of Memory Exception,读取相关问题并将其更改为字符串+ =,但仍然收到相同的OOM错误。字符串和StringBuilder上的内存不足
我有〜2100多个不同大小的文件和总计〜200MB +的文件。
由于限制,我无法使用追加模式文件访问。任何的霸王龙?
const string myPath = @"C:\values\";
var files = Directory.EnumerateFiles(myPath).Select(Path.GetFileNameWithoutExtension);
var filesGrouped = files.Where(f => f.Length > 12).GroupBy(f => f.Substring(0, f.Length - 9)).OrderBy(f => f.Key).ToList();
filesGrouped.ForEach(fg =>
{
var myRows = string.Empty;
// download files
fg.ToList().ForEach(gf =>
{
string myFile;
string fileToRead = string.Format("{0}{1}.csv", myPath, gf);
using (var sr = new StreamReader(fileToRead)) myFile = sr.ReadToEnd();
//! OOM Error hits the line below
myRows += myFile + ControlChars.NewLine;
});
// upload new file
using (var sw = new StreamWriter(myPath + fg.Key + ".csv", false, Encoding.UTF8)) sw.Write(myRows);
});
旁注:用'Environment.NewLine'替换'ControlChars.NewLine'。 – 2013-03-11 14:37:07