ASP.NET 5 / MVC 6 /C#: Close File Path after usage -


i've got problem closing temporary file. in method i'm, generating ics file, write text in , send using mimemail. problem don't know how close path can access delete after mail send. , mimemail not provide solution message.dispose() or message.close().

here code:

public void sendemailwithical(string toemailaddress, string subject, string textbody)     {         var message = new mimemessage();         message.from.add(             new mailboxaddress("chronicus dev", this.username));          message.to.add(new mailboxaddress(toemailaddress.trim(), toemailaddress.tostring()));          message.subject = subject;          calenderitems icalender = new calenderitems();         icalender.generateevent("neuer kalendereintrag");          var termin = icalender.ical;          string path = "tempicsfiles/file.ics";         if (file.exists(path))         {             file.delete(path);         }         {             // create file , write             filestream fs = new filestream(path, filemode.openorcreate);             streamwriter str = new streamwriter(fs);             str.basestream.seek(0, seekorigin.end);             str.write(termin.tostring());              //close filestream , streamwriter             str.flush();             str.dispose();             fs.dispose();                              //add attachment             var attachment = new mimepart("image", "gif")             {                 contentobject = new contentobject(file.openread(path), contentencoding.default),                 contentdisposition = new contentdisposition(contentdisposition.attachment),                 contenttransferencoding = contentencoding.base64,                 filename = path.getfilename(path)             };              var body = new textpart("plain")             {                 text = "meine ical mail"             };             //configure email             var multipart = new multipart("mixed");             multipart.add(body);             multipart.add(attachment);             message.body = multipart;              //send email             using (var client = new smtpclient())             {                 client.connect(hostname, port, false);                 client.authenticate(username, password);                 client.send(message);                 client.disconnect(true);             }              //todo close file              //trying delete, exception             file.delete(path);         }     } 

thanks help!

try relocate file.openread(path), , wrap whole message object in using() this:

public void sendemailwithical(string toemailaddress, string subject, string textbody) {     calenderitems icalender = new calenderitems();     icalender.generateevent("neuer kalendereintrag");     var termin = icalender.ical;     string path = "tempicsfiles/file.ics";     if (file.exists(path))     {         file.delete(path);     }     //create file , write     using (var fs = new filestream(path, filemode.openorcreate))     {         using (var str = new streamwriter(fs))         {             str.basestream.seek(0, seekorigin.end);             str.write(termin.tostring());             //close filestream , streamwriter             str.flush();         }     }     //compose message     using (var read_stream = file.openread(path))     {         using (var message = new mimemessage())         {             message.from.add(new mailboxaddress("chronicus dev", this.username));             message.to.add(new mailboxaddress(toemailaddress.trim(), toemailaddress.tostring()));             message.subject = subject;             //add attachment             var attachment = new mimepart("image", "gif")             {                 contentobject = new contentobject(read_stream, contentencoding.default),                 contentdisposition = new contentdisposition(contentdisposition.attachment),                 contenttransferencoding = contentencoding.base64,                 filename = path.getfilename(path)             };              var body = new textpart("plain")             {                 text = "meine ical mail"             };             //configure email             var multipart = new multipart("mixed");             multipart.add(body);             multipart.add(attachment);             message.body = multipart;              //send email             using (var client = new smtpclient())             {                 client.connect(hostname, port, false);                 client.authenticate(username, password);                 client.send(message);                 client.disconnect(true);             }         }     }     //delete temporary file     file.delete(path); } 

this should guarantee closed file, assuming client.send entirely synchronous operation.

see possible duplicate https://stackoverflow.com/questions/1296380/smtp-send-is-locking-up-my-files-c-sharp


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -