[转]c# xml.Load() locking file on disk causing errors
本文转自:http://stackoverflow.com/questions/1812598/c-sharp-xml-load-locking-file-on-disk-causing-errors
问:
I have a simple class XmlFileHelper as follows:
publicXmlFileHelper(string xmlFilePath)
{
this.xmlFilePath = xmlFilePath;
xmlDoc.Load(xmlFilePath);
}
this occurs when this class is used to by two running instances
of a component running in parallel both attempting to load the xml file above,
this is legitimate behaviour and required by the application.
答1:
You can do this
using(Stream s =File.OpenRead(xmlFilePath)){
xmlDoc.Load(s);}
instead of
xmlDoc.Load(xmlFilePath);
答2:
FileStream xmlFile =newFileStream(xmlFilePath,FileMode.Open,
FileAccess.Read,FileShare.Read);
xmlDoc.Load(xmlFile);
posted on 2013-12-31 09:55 freeliver54 阅读(318) 评论(0) 编辑 收藏 举报
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步