Thursday, 15 March 2012

vb.net - Unable to delete PDF file after sending email -



vb.net - Unable to delete PDF file after sending email -

my code process info website exports word documents converts pdf , proceeds attach onto email code. i've managed send email pdf have problems file management. in case want delete files in folder namely, generated word , pdf document. code runs , able delete word document unable delete pdf document.

the error when manually tried delete file "the action can't completed because file open in webdev.webserver40.exe". running on debug mode , wish run on iis in future.

below snippet of email code

dim smtp_server new smtpclient dim e_mail new mailmessage() smtp_server.usedefaultcredentials = false e_mail = new mailmessage() e_mail.subject = "job completed " e_mail.isbodyhtml = false e_mail.body = msg e_mail.attachments.add(new attachment(server.mappath("pdf\" + filename + ".pdf"))) smtp_server.send(e_mail) smtp_server.dispose() e_mail.attachments.clear() e_mail.dispose()

removed authentication , sending address. below code delete files

dim filepaths string() = directory.getfiles(httpcontext.current.server.mappath("pdf")) each filepath string in filepaths file.delete(filepath) next

below class file used convert pdf

imports microsoft.visualbasic imports system.io imports microsoft.office.interop.word public class convertwordtopdf public sub converttopdf(strfilename string) ' create new microsoft word application object dim word new microsoft.office.interop.word.application() ' c# doesn't have optional arguments we'll need dummy value dim omissing object = system.reflection.missing.value ' list of word files in specified directory dim dirinfo new directoryinfo(httpcontext.current.server.mappath("pdf")) dim wordfiles fileinfo() = dirinfo.getfiles("*.docx") word.visible = false word.screenupdating = false ' cast object word open method dim filename [object] = directcast(wordfiles(0).fullname, [object]) 'dim filename object = strfilename ' utilize dummy value placeholder optional arguments dim doc document = word.documents.open(filename, omissing, omissing, omissing, omissing, omissing, _ omissing, omissing, omissing, omissing, omissing, omissing, _ omissing, omissing, omissing, omissing) doc.activate() dim outputfilename object = wordfiles(0).fullname.replace(".docx", ".pdf") dim fileformat object = wdsaveformat.wdformatpdf ' save document pdf format doc.saveas(outputfilename, fileformat, omissing, omissing, omissing, omissing, _ omissing, omissing, omissing, omissing, omissing, omissing, _ omissing, omissing, omissing, omissing) ' close word document, leave word application open. ' doc has cast type _document find ' right close method. dim savechanges object = wdsaveoptions.wddonotsavechanges directcast(doc, _document).close(savechanges, omissing, omissing) doc = nil ' word has cast type _application find ' right quit method. directcast(word, _application).quit(omissing, omissing, omissing) word = nil end sub end class

edit: added code check if file locked

protected overridable function isfilelocked(file fileinfo) boolean dim stream filestream = nil seek stream = file.open(filemode.open, fileaccess.readwrite, fileshare.none) grab generatedexceptionname ioexception 'the file unavailable because is: 'still beingness written 'or beingness processed thread 'or not exist (has been processed) homecoming true if stream isnot nil stream.close() end if end seek 'file not locked homecoming false end function

it returned false after pdf created.

just tested scenario able reproduce problem. able prepare adding attachment stream rather path file. remember close stream 1 way or another.

this code gives error descriped:

dim smtp new smtpclient("...") message.attachments.add(new attachment("c:\myfile.txt")) smtp.send(message) file.delete("c:\myfile.txt")

this works:

dim smtp new smtpclient("...") using reader new streamreader("c:\myfile.txt") dim new attachment(reader.basestream, "myfile.txt") message.attachments.add(a) smtp.send(message) end using file.delete("c:\myfile.txt")

vb.net visual-studio-2010 file email email-attachments

No comments:

Post a Comment