例子:
variable = new StreamReader( file ).ReadToEnd();
可以接受吗?
最佳答案
不,这不会关闭StreamReader。您需要关闭它。使用为您执行此操作(并进行处理,以便GC尽快发布):
using (StreamReader r = new StreamReader("file.txt"))
{
allFileText = r.ReadToEnd();
}
或者,在.Net 2中,您可以使用新文件。静态成员,那么您无需关闭任何内容:
variable = File.ReadAllText("file.txt");
关于c# - 使用它将文件加载到字符串变量中时,是否需要在C#中显式关闭StreamReader?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4136490/