c# - OutOfMemory exception thrown while writing large text file -
i want generate string , write in .txt file. problem outofmemory exceptions when attempt so.
the file large (about 10000 lines).
i use string.format
, loops create string. how can write .txt file?
string text= @"..."; const string channelscalar = @"..."; text= string.format(...); foreach (channel channel in ...) { switch (channel.type) { case "...": text= string.format(text, channelframes(channel, string.format(...); break; } } file.writealltext(textbox9.text,text);
use streamwriter
directly write each line generate textfile. avoids storing whole long file in memory first.
using (system.io.streamwriter sw = new system.io.streamwriter("c:\\somewhere\\whatever.txt")) { //generate single lines , write them directly file (int = 0; i<=10000;i++) { sw.writeline("this such nice line of text. *snort*"); } }
Comments
Post a Comment