C# Exception: The process cannot access the file


i'm getting exception: process cannot access file.

here's snippet code:

if (!monitor.tryenter(lockobject))
   
return;
try
{
    watcher
.enableraisingevents = false;
   
try
   
{
       
xmldocument xdoc = new xmldocument();
        xdoc
.load(filename);
        xdoc
= null;
   
}
   
catch (xmlexception xe)
   
{
        using
(streamwriter w = file.appendtext(filename))
       
{
           
console.writeline(xe);
            w
.writeline("</test>");
            w
.writeline("</testwrapper>");
       
}
   
}
   
system.threading.thread.sleep(2000);
   
xpathdocument myxpathdoc = new xpathdocument(new streamreader(filename, system.text.encoding.getencoding("windows-1256")));
   
xslcompiledtransform myxsltrans = new xslcompiledtransform();
    myxsltrans
.load("d:/gs/xsl/test.xsl");
   
xmltextwriter mywriter = new xmltextwriter(destinationfile, null);
    mywriter
.formatting = formatting.indented;
    mywriter
.indentation = 4;
    myxsltrans
.transform(myxpathdoc, null, mywriter);
    mywriter
.close();
}
catch (exception e)
{
   
console.writeline("the process failed: {0}", e.tostring());
}
finally
{
   
monitor.exit(lockobject);
    watcher
.enableraisingevents = true;
    gc
.collect();
    gc
.waitforpendingfinalizers();
    gc
.collect();
}

the code working before added these lines. these testing if xml file without closing tags (which) add tags. after added following code started giving me exception.

try
{
   
xmldocument xdoc = new xmldocument();
    xdoc
.load(filename);
    xdoc
= null;

}
catch (xmlexception xe)
{
    using
(streamwriter w = file.appendtext(filename))
   
{
       
console.writeline(xe);
        w
.writeline("</test>");
        w
.writeline("</testwrapper>");
   
}
}  

what wrong here ?

here error i'm getting :

the process failed: system.io.ioexception: process cannot access file 'z :\tf_b1bba.xml' because being used process. @ system.io.__error.winioerror(int32 errorcode, string maybefullpath) @ system.io.filestream.init(string path, filemode mode, fileaccess access, nt32 rights, boolean userights, fileshare share, int32 buffersize, fileoptions o ptions, security_attributes secattrs, string msgpath, boolean bfromproxy, boolea n uselongpath) @ system.io.filestream..ctor(string path, filemode mode, fileaccess access, fileshare share, int32 buffersize) @ system.xml.xmldownloadmanager.getstream(uri uri, icredentials credentials, iwebproxy proxy, requestcachepolicy cachepolicy) @ system.xml.xmlurlresolver.getentity(uri absoluteuri, string role, type ofo bjecttoreturn) @ system.xml.xmltextreaderimpl.openurldelegate(object xmlresolver) @ system.threading.compressedstack.runtrycode(object userdata) @ system.runtime.compilerservices.runtimehelpers.executecodewithguaranteedcl eanup(trycode code, cleanupcode backoutcode, object userdata) @ system.threading.compressedstack.run(compressedstack compressedstack, cont extcallback callback, object state) @ system.xml.xmltextreaderimpl.openurl() @ system.xml.xmltextreaderimpl.read() . @ system.xml.xmlloader.load(xmldocument doc, xmlreader reader, boolean prese vewhitespace) @ system.xml.xmldocument.load(xmlreader reader) @ system.xml.xmldocument.load(string filename) @ gselinterface.program.convert(object source, filesystemeventargs f) in c:\ documents , settings\administrator\desktop\consoleapplication1\consoleapplicat ion1\program.cs:line 178

may file opened in other thread? or program?


Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

BizTalk Server 2013 Azure VM Log Shipping and HA for hosts

How to Share webservice object to all user