Cannot access file after writing it!

D

David Lozzi

Howdy,

I am using ASP.Net 2.0 and Crystal Reports in VS 2005. I am taking a report,
converting it to PDF then writing it out to a folder. Then I attach this
file to an email and send it off. Once it's sent, I then want to delete the
file from the folder, and here lies the issue. When i try to delete it I get
an error stating the file is being used by another process.

Here's the code to create the file

Dim strStream As New
System.IO.BinaryReader(crReport.ExportToStream(CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat))

Dim fileName As String = "WO-" & Request("woid") & ".pdf"
Dim fs As FileStream = File.Create("c:\temp\" & fileName,
strStream.BaseStream.Length)

Dim writer As New IO.BinaryWriter(fs)
writer.Write(strStream.ReadBytes(strStream.BaseStream.Length))
writer.Flush()
writer.Close()

fs.Dispose()
fs.Close()
strStream.Close()
crReport.Close()

and to delete I simply

File.Delete(filePath)


Thanks a million!
David Lozzi
 
A

Alex Meleta

Hi David,

PS. Also it's a good way to use "using statement" for disposable objects
to be sure that object will be freed.

Using cn As Dim fs As FileStream = File.Create
// do something
End Using

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com



DL> Howdy,
DL>
DL> I am using ASP.Net 2.0 and Crystal Reports in VS 2005. I am taking a
DL> report, converting it to PDF then writing it out to a folder. Then I
DL> attach this file to an email and send it off. Once it's sent, I then
DL> want to delete the file from the folder, and here lies the issue.
DL> When i try to delete it I get an error stating the file is being
DL> used by another process.
DL>
DL> Here's the code to create the file
DL>
DL> Dim strStream As New
DL> System.IO.BinaryReader(crReport.ExportToStream(CrystalDecisions.[Sha
DL> red].ExportFormatType.PortableDocFormat))
DL>
DL> Dim fileName As String = "WO-" & Request("woid") &
DL> ".pdf"
DL> Dim fs As FileStream = File.Create("c:\temp\" &
DL> fileName,
DL> strStream.BaseStream.Length)
DL> Dim writer As New IO.BinaryWriter(fs)
DL>
DL> writer.Write(strStream.ReadBytes(strStream.BaseStream.Length))
DL> writer.Flush()
DL> writer.Close()
DL> fs.Dispose()
DL> fs.Close()
DL> strStream.Close()
DL> crReport.Close()
DL> and to delete I simply
DL>
DL> File.Delete(filePath)
DL>
DL> Thanks a million!
DL> David Lozz
 
V

vMike

David Lozzi said:
Howdy,

I am using ASP.Net 2.0 and Crystal Reports in VS 2005. I am taking a
report, converting it to PDF then writing it out to a folder. Then I
attach this file to an email and send it off. Once it's sent, I then want
to delete the file from the folder, and here lies the issue. When i try to
delete it I get an error stating the file is being used by another
process.

Here's the code to create the file

Dim strStream As New
System.IO.BinaryReader(crReport.ExportToStream(CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat))

Dim fileName As String = "WO-" & Request("woid") & ".pdf"
Dim fs As FileStream = File.Create("c:\temp\" & fileName,
strStream.BaseStream.Length)

Dim writer As New IO.BinaryWriter(fs)
writer.Write(strStream.ReadBytes(strStream.BaseStream.Length))
writer.Flush()
writer.Close()

fs.Dispose()
fs.Close()
strStream.Close()
crReport.Close()

and to delete I simply

File.Delete(filePath)


Thanks a million!
David Lozzi
In your email sending code call the attachments dispose method

yourMailMessage.attachments.dispose()

Mike
 
D

David Lozzi

Can I have your love child? That worked. Another dev and I have been chasing
down the stupid filestream object and what not and it was the stupid
message.... Thank you for your generousity and time!

David Lozzi

vMike said:
David Lozzi said:
Howdy,

I am using ASP.Net 2.0 and Crystal Reports in VS 2005. I am taking a
report, converting it to PDF then writing it out to a folder. Then I
attach this file to an email and send it off. Once it's sent, I then want
to delete the file from the folder, and here lies the issue. When i try
to delete it I get an error stating the file is being used by another
process.

Here's the code to create the file

Dim strStream As New
System.IO.BinaryReader(crReport.ExportToStream(CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat))

Dim fileName As String = "WO-" & Request("woid") & ".pdf"
Dim fs As FileStream = File.Create("c:\temp\" & fileName,
strStream.BaseStream.Length)

Dim writer As New IO.BinaryWriter(fs)
writer.Write(strStream.ReadBytes(strStream.BaseStream.Length))
writer.Flush()
writer.Close()

fs.Dispose()
fs.Close()
strStream.Close()
crReport.Close()

and to delete I simply

File.Delete(filePath)


Thanks a million!
David Lozzi
In your email sending code call the attachments dispose method

yourMailMessage.attachments.dispose()

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top