有没有办法做到这一点:
this.logFile = File.Open("what_r_u_doing.log", FileMode.OpenOrCreate, FileAccess.ReadWrite);
using(var sr = new StreamReader(this.logFile))
{
// Read the data in
}
// ... later on in the class ...
this.logFile = File.Open("what_r_u_doing.log", FileMode.OpenOrCreate, FileAccess.ReadWrite);
using(var sw = new StreamWriter(this.logFile))
{
// Write additional data out...
}
Run Code Online (Sandbox Code Playgroud)
无需打开文件两次?
我似乎无法使StreamReader不处理我的流.我也不想让它超出范围.然后垃圾收集器最终将调用Dispose,杀死流.