c芬xml.Load()locking file on disk causing errors

1582 ワード

本論文は、http://stackoverflow.com/questions/1812598/c-sharp-xml-load-locking-file-on-disk-causing-errorsから回転する。
質問:
I have a simple class Xml FileHelper as follows:
publicXml FileHelper(string xml FilePath)    {        this.xml FilePath=xml FilePath;        xml Doc.Load(xml FilePath)    }
this occurs when this class to by two running instancs
of a component running in parallel both atempting to load the xml file aboove、
this is legitimate behaviour and required by the application.
 
答1:
You can do this
using(Stream s =File.OpenRead(xmlFilePath)){ xmlDoc.Load(s);}
instead ofxmlDoc.Load(xmlFilePath); A 2:FileStream xml File=newFileStream(xml FilePath、FileMode.Open、FileAccess.Read、FileShare.Read);            xml Doc.Load(xml File);