文件打开内存c#

Pra*_*ant 0 c#

使用流阅读器在C#中打开文件时,文件将保留在内存中直到关闭.例如,如果程序使用streamreader打开大小为6MB的文件,则在文件末尾附加一行.程序是否将整个6 MB保留在内存中,直到文件关闭.OR是.Net代码在内部返回的文件指针,并在末尾附加行.因此程序不会占用6MB内存

dan*_*ben 5

流的整个要点是,您不必将整个对象保存在内存中.你可以根据需要逐一阅读.

如果要附加到文件,则应使用File.AppendText哪个文件将创建StreamWriter附加到文件末尾的文件.

这是一个例子:

 string path = @"c:\temp\MyTest.txt";

 // This text is always added, making the file longer over time
 // if it is not deleted.
 using (StreamWriter sw = File.AppendText(path)) 
 {
     sw.WriteLine("This");
     sw.WriteLine("is Extra");
     sw.WriteLine("Text");
 } 
Run Code Online (Sandbox Code Playgroud)

同样,整个文件不会存储在内存中.

文档:http: //msdn.microsoft.com/en-us/library/system.io.file.appendtext.aspx