 |
| ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

December 17th, 2009, 06:45 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 107
Thanks: 1
Thanked 8 Times in 7 Posts
|
|
Create XML File, Email it, Delete It
Hi,
Can anyone suggest any way of creating an XML file, emailing it as an attachment and then deleting the original file.
I can create the xml file and delete it wothout any problem
I can create it and email it without any problem
However I can't create, email, then delete it without the following error
Code:
The process cannot access the file '.../filename.xml' because it is being used by another process
I am flushing, disposing and closing every object before the file deletion but I just can't get the deletion to work until whichever process still views it as open has closed.
If you have any suggestions or alternative methods they will be gratefully received.
Thanks
Tim.
|
|

December 21st, 2009, 11:46 AM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
You need to call Dispose() on your MailMessage object after you send the email otherwise it keeps ahold of the file you are sending as an attachment.
hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click  on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
|
|

December 22nd, 2009, 05:51 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 107
Thanks: 1
Thanked 8 Times in 7 Posts
|
|
Thanks for your reply, but I can't see which bit I'm not closing.
If I try the following code It creates and then deletes the xml file.
(For convenience I've reduced the code as much as possible and running it on a local host)
Code:
Dim XMLFileName AsString
XMLFileName = "C:\data\NewFile.xml"
' Create a new XmlTextWriter instance
Dim writer AsNew XmlTextWriter(XMLFileName, Encoding.UTF8)
' Dim writer As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
' start writing!
writer.WriteStartDocument()
writer.WriteStartElement("userInfo")
' Creating the <browserInfo> element
writer.WriteStartElement("browserInfo")
If Request.UrlReferrer IsNothingThen
writer.WriteElementString("urlReferrer", "none")
Else
writer.WriteElementString("urlReferrer", Request.UrlReferrer.PathAndQuery)
EndIf
writer.WriteElementString("userAgent", Request.UserAgent)
writer.WriteElementString("userLanguages", [String].Join(", ", Request.UserLanguages))
writer.WriteEndElement()
' -------------------------------------
' End Client Data
' -------------------------------------
writer.WriteEndElement()
writer.WriteEndDocument()
writer.Flush()
writer.Close()
' -----------------------------------------------------------------------------
' Need to Delete the XML File XMLFileName
' -----------------------------------------------------------------------------
File.Delete(XMLFileName)
If I then add in the email function then the File.Delete fails to work.
Code:
Dim XMLFileName AsString
XMLFileName = "C:\data\NewFile.xml"
' Create a new XmlTextWriter instance
Dim writer AsNew XmlTextWriter(XMLFileName, Encoding.UTF8)
' Dim writer As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
' start writing!
writer.WriteStartDocument()
writer.WriteStartElement("userInfo")
' Creating the <browserInfo> element
writer.WriteStartElement("browserInfo")
If Request.UrlReferrer IsNothingThen
writer.WriteElementString("urlReferrer", "none")
Else
writer.WriteElementString("urlReferrer", Request.UrlReferrer.PathAndQuery)
EndIf
writer.WriteElementString("userAgent", Request.UserAgent)
writer.WriteElementString("userLanguages", [String].Join(", ", Request.UserLanguages))
writer.WriteEndElement()
' -------------------------------------
' End Client Data
' -------------------------------------
writer.WriteEndElement()
writer.WriteEndDocument()
writer.Flush()
writer.Close()
' ---------------------------------------------------------------------------
' Send an email
' ---------------------------------------------------------------------------
Dim sBody AsString = "This is the message for the email"
Dim MailObj AsNew Net.Mail.SmtpClient()
' -----------------------------------------------------------------------------
'Sub MultiPartMime()
' -----------------------------------------------------------------------------
' -----------------------------------------------------------------------------
' First we create the Plain Text part
' -----------------------------------------------------------------------------
Dim xa AsString
xa = sBody
xa = Regex.Replace(xa, "<(.|\n)*?>", String.Empty)
Dim plainView As Net.Mail.AlternateView = Net.Mail.AlternateView.CreateAlternateViewFromString(xa, Nothing, "text/plain")
' -----------------------------------------------------------------------------
'then we create the Html part
' -----------------------------------------------------------------------------
Dim htmlView As Net.Mail.AlternateView = Net.Mail.AlternateView.CreateAlternateViewFromString(sBody, Nothing, "text/html")
Dim commandResult1 AsInteger = 0
Dim MailAttachment AsNew System.Net.Mail.Attachment(XMLFileName)
Dim Msg AsNew System.Net.Mail.MailMessage("[email protected]", "[email protected]")
Msg.Attachments.Add(MailAttachment)
Msg.From = New Net.Mail.MailAddress("[email protected]")
Msg.Subject = "EMail Title"
Msg.AlternateViews.Add(plainView)
Msg.AlternateViews.Add(htmlView)
Msg.IsBodyHtml = True
'send the message
Dim smtp1 AsNew Net.Mail.SmtpClient
commandResult1 = 1
Try
smtp1.Send(Msg)
Catch ex As Exception
Session("EMAILFAILCOUNT") = Session("EMAILFAILCOUNT") + 1
If Session("EMAILFAILCOUNT") > 10 Then
Session("EMAILFAILCOUNT") = 0
commandResult1 = ex.Message
Session("ErrorMessage") = " Sorry there has been an error - Error " + commandResult1.ToString
Session("RRdirect") = "NO RETRY"
Session("ErrorMessageSQL") = ex.Message
Response.Redirect("Report.aspx")
ExitSub
Else
smtp1.Send(Msg)
EndIf
Msg.Dispose()
MailAttachment.Dispose()
EndTry
plainView.Dispose()
htmlView.Dispose()
' -----------------------------------------------------------------------------
' End Sub 'MultiPartMime
' -----------------------------------------------------------------------------
' -----------------------------------------------------------------------------
' Need to Delete the XML File ArchiveFileLabel.Text
' -----------------------------------------------------------------------------
File.Delete(XMLFileName)
The message displayed is
The process cannot access the file 'C:\data\NewFile.xml' because it is being used by another process
and references the line.
File.Delete(XMLFileName)
The xml file is created correctly and is delivered as an attachment to the email. But for the life of me I can't see which object I'm failing to close.
Any help gratefully received.
|
|

December 22nd, 2009, 06:38 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
You're disposing your objects in the Catch block, which means they are only disposed off in case of a failure. Move the .Dispose calls to a Finally block and it should work...
Code:
Try
' Do stuff here
Catch ex As Exception
' Handle error here
Finally
' Call Dispose and other clean up actions here
End Try
Cheers,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

December 22nd, 2009, 08:08 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 107
Thanks: 1
Thanked 8 Times in 7 Posts
|
|
Thanks Imar,
I just couldn't see it for looking.
I hope you have a great Christmas, and thanks for allowing us all to read your answers over the last year.
(The books a quite good too)
|
|
 |