Click to See Complete Forum and Search --> : How to Delete this temp file


Nate1
03-25-2008, 05:56 PM
I have built this sub to retrieve information off an email form and place it into a number of different email templates, this works well, though the sub in question creates a temp file (not actual temp directory - kept saying used by another process?) I have to use response.end else the response serves the entire web page not just the email.htm preview. How can I delete this temp file, so I can then use a unique ID to make sure two users don't have conflicts?

Dim FileName As String = Server.MapPath(Utility.AppSettings.TemplateFolderPath & "fil.tmp")
Dim f As New FileInfo(ToText(Utility.AppSettings.TemplateFolderPath & ddlEmailTemplates.SelectedValue))
Dim e As TemplateEmail = getTemplateInformation()
Dim info As String = ParseEmailTemplate(e, Server.MapPath(Utility.AppSettings.TemplateFolderPath) & ddlEmailTemplates.SelectedValue)

Dim fs As New FileStream(FileName, FileMode.Create, FileAccess.Write)
Dim s As New StreamWriter(fs)

s.WriteLine(info)
s.Close()
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.Charset = ""
HttpContext.Current.Response.ContentType = "application/octet-stream"
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Email_Preview" & f.Extension & "l")

Try
HttpContext.Current.Response.WriteFile(FileName)
Finally
'Dim delfile As New FileInfo(FileName)
'delfile.Delete()
Response.End()

End Try
'Then Delete the File

chazzy
03-26-2008, 02:12 PM
hmmm.. do you want to delete the file after they've reviewed it, or after it's sent? couldn't you just put it into the temp directory?

Nate1
03-26-2008, 09:10 PM
I could stick it in the Temp directory which is what I tried at first but it wouldn't work, not sure why kept saying file used by another process???

chazzy
03-26-2008, 10:00 PM
hmmm.. it's the file that you're using in FileStream, correct? how come you're never closing FileStream ?

Nate1
03-27-2008, 01:33 AM
I don't know? Garbage Collector probably does it, but Im still going to be left with a useless file, that I want to delete? I would like to assign Ids to the file so I can be sure that a number of users can use the system at the same time but currently this setup is stopping that so I need to be able to delete this file/s or use the temp directory, if you have code similar to this which uses the temp directory and works then that would be helpful?

chazzy
03-27-2008, 05:53 AM
The fact that you're nto closing FileStream is the issue, I believe. Try closing FS before you delete the file and see if it still claims to have something attached to it.

Nate1
03-27-2008, 02:57 PM
Yeah, I tried it but the response.end which is required to feed the preview out only and not the entire response, stops the file delete from working, Will try with a Temp file again.


Dim fs As New FileStream(FileName, FileMode.Create, FileAccess.Write)

Dim s As New StreamWriter(fs)

s.WriteLine(info)
s.Close()

HttpContext.Current.Response.Clear()
HttpContext.Current.Response.Charset = ""
HttpContext.Current.Response.ContentType = "application/octet-stream"
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Email_Preview" & f.Extension & "l")

Try

Catch
HttpContext.Current.Response.WriteFile(FileName)
Response.End()
fs.Close()
Dim delfile As New FileInfo(FileName)
delfile.Delete()
Finally

thrill6
03-30-2008, 03:29 AM
Why write the data to a file when you are going to delete it?

You should be able to:

HttpContext.Current.Response.Write(info)

Nate1
03-30-2008, 02:31 PM
Honestly Can't remember

But his does what I want
Protected Sub showTemplate()
Dim FileName As String = System.IO.Path.GetTempFileName()
Dim f As New FileInfo(ToText(Utility.AppSettings.TemplateFolderPath & ddlEmailTemplates.SelectedValue))
Dim e As TemplateEmail = getTemplateInformation(True)
Dim info As String = ParseEmailTemplate(e, Server.MapPath(Utility.AppSettings.TemplateFolderPath) & ddlEmailTemplates.SelectedValue)

Dim fs As New FileStream(FileName, FileMode.Create, FileAccess.Write)
Dim s As New StreamWriter(fs)

s.WriteLine(info)
s.Close()

HttpContext.Current.Response.Clear()
HttpContext.Current.Response.Charset = ""
HttpContext.Current.Response.ContentType = "application/octet-stream"
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Email_Preview" & f.Extension & "l")

HttpContext.Current.Response.WriteFile(FileName)
Response.End()
End Sub