c# - 为什么 FileStream 没有被 XmlReader 关闭

标签 c# .net idisposable xmlreader

所以我在 XmlReader 中使用 FileStream

using (XmlReader reader = XmlReader.Create(new FileStream(archivePath, FileMode.Open), readerSettings))
{
    reader.close()
}

但是,XmlReader 中的文件在 using 作用域后仍然处于锁定状态,很奇怪,我认为 XmlReader 是要为我关闭 FileStream,是吗?

感谢您的帮助。

最佳答案

您应该能够通过 XmlReaderSettings.CloseInput 控制它。

readerSettings.CloseInput = true;
using (XmlReader reader = XmlReader.Create(new FileStream(archivePath, FileMode.Open), readerSettings))
{
    // do work with the reader
}

或者,如果您不关心其他阅读器设置,则更简洁:

using (XmlReader reader = XmlReader.Create(new FileStream(archivePath, FileMode.Open), new XmlReaderSettings() { CloseInput = true }))
{
    // do work with the reader
}

关于c# - 为什么 FileStream 没有被 XmlReader 关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9844978/

相关文章:

c# - 无法将类型从 string 更改为 int?

c# - UWP XAML - 将 GridView 的大小调整为 ListViewItem 父级的完整宽度

c# - 不能在 Using 语句中使用通用 C# 类

c# - 为什么 HashAlgorithm 类要实现 IDisposable?

c# - 使用自定义 TTF 字体进行 DrawString 图像渲染

C# "using"语句中的匿名对象可靠吗?

c# - 我们如何以及在哪里编写 try catch block 来处理异常

c# - 要增加的 Int session 变量?

c# - 如何制作我的应用程序单例应用程序?

.net - 将简单字符串参数传递给 .net webservice 时出现问题