c# - The process cannot access the file filename because it is being used by another process -


i generating pdfs code:

foreach (var emp in emplist) {     ....         byte[] bytes;     using (var ms = new memorystream())     {          //create itextsharp document abstraction of pdf **not** pdf         using (var doc = new document())         {              //create writer that's bound our pdf abstraction , our stream             using (var writer = pdfwriter.getinstance(doc, ms))             {                 //open document writing                 doc.open();                  using (var htmlworker = new itextsharp.text.html.simpleparser.htmlworker(doc))                 {                      //htmlworker doesn't read string directly instead needs textreader (which stringreader subclasses)                     using (var sr = new stringreader(emailbody))                     {                          //parse html                         htmlworker.parse(sr);                     }                 }                  doc.close();             }         }          bytes = ms.toarray();     }      bool isexist = system.io.directory.exists(server.mappath("~/" + session["schemaname"].tostring() + "/hrletters"));     if (!isexist)     {         system.io.directory.createdirectory(server.mappath("~/" + session["schemaname"].tostring() + "/hrletters"));     }     system.io.file.writeallbytes(server.mappath("~/" + session["schemaname"].tostring() + "/hrletters/" + emp.code.tostring() + ".pdf"), bytes.toarray()); } 

then send pdf files attachment through mail code:

....... smtpclient smtp = new smtpclient                 {                     host = data.smtpserver, // smtp server address here...                                         port = data.portno,                     enablessl = data.ssl,                     deliverymethod = smtpdeliverymethod.network,                     credentials = new system.net.networkcredential(senderid, senderpassword),                     timeout = 30000,                 };                 thread th = new thread(() => { smtp.send(message); });                 th.start(); 

then try delete folder:

if (system.io.directory.exists(server.mappath("~/" + session["schemaname"].tostring()))) {     system.io.directory.delete(server.mappath("~/" + session["schemaname"].tostring()), true); } 

i error:

the process cannot access file '001.pdf' because being used process.

how solve issue? happening because of thread running @ time of mail send?

some handle still open pdf file while try delete in main thread. should delete them in sending thread.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -