【发布时间】:2016-04-09 20:36:55
【问题描述】:
我正在使用 SharpCompress 库(这不是问题)。我的拉链要么未受保护,要么受密码保护(都使用相同的密码)。所以我是这样编码的:
using(Stream stream = File.OpenRead(file))
{
try {
reader = ZipReader.Open(stream);
moreFiles = reader.MoveToNextEntry();
} catch (Exception e) {
reader = ZipReader.Open(stream, pwd);
moreFiles = reader.MoveToNextEntry();
}
//rest of code
}
每当它到达 catch 块调用 MoveToNextEntry 时,它总是会引发一个模糊的“无效标头:xxxxxxxx”异常,因为我认为流不在开始处。
我不能将File.OpenRead(file) 放入异常中,因为我正在使用using 块。如果我使用(如 cmets 中所建议的)搜索,我会得到一个未设置的对象引用(可能已调用 dispose)。
有没有办法重置这个文件流并仍然确保它被释放(同时仍然使用using 块)。我不知道再次调用 open 是否会导致 using 块也有点不高兴。
谢谢。
【问题讨论】:
标签: c# sharpcompress