c# - Cannot access to simple file "because it is being used by another process" -
i have simple method in class edidocument used load xml :
/// <summary> /// method used load specific target file template /// </summary> /// <param name="filepath">file path</param> /// <returns>loading status</returns> public bool load(string filepath) { //file exists bool returnvalue = file.exists(filepath); //file xml returnvalue &= path.getextension(filepath).equals(".xml"); //if file valid if (returnvalue) { xmlreader reader = xmlreader.create(filepath); //load document this._xmldoc = xdocument.load(reader); //load complete returnvalue &= (this._xmldoc != null); } //end of method return returnvalue; }
i have unit test method :
/// <summary> /// test success on load xml document /// </summary> [testmethod] public void testloadxml_success() { file.create("xml.xml"); //create document edidocument doc = new edidocument(); //load wrong bool result = doc.load("xml.xml"); //test assert.istrue(result); }
i have exception when start unit test :
test method edidocumenttest.testloadxml_success threw exception: system.io.ioexception: process cannot access file 'c:......\debug\xml.xml' because being used process.
i have googled issue , have tried multiple solution xmlreader, streamreader... have same exception...
my question : method load fix exception?
thanks
file.create
returns stream file, keeps handle open it. need close file first. should work:
file.create("xml.xml").close();
see question more details: why file.create needed closed?
Comments
Post a Comment