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

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -