using中StreamWriter XmlWriter 区别
使用StreamWriter
using (var writer = new StreamWriter(File.Create(path))) { writer.WriteLine("sdf"); }
跳出using后stream也释放了
using (XmlWriter xw = XmlWriter.Create( File.Create(path), new XmlWriterSettings() { OmitXmlDeclaration = true })) { element.Save(xw); }
跳出using后stream没释放。。。
解决方法很简单,using套using,或者XmlWriterSettings中CloseOutput=true。
.net 4.5中StreamWriter添加了新的构造函数,可以设置参数leaveOpen为true,stream也不会释放