Cannot access file after writing it!

Discussion in 'ASP .Net' started by David Lozzi, Jul 10, 2007.

  1. David Lozzi

    David Lozzi Guest

    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
     
    David Lozzi, Jul 10, 2007
    #1
    1. Advertising

  2. David Lozzi

    Alex Meleta Guest

    Alex Meleta, Jul 10, 2007
    #2
    1. Advertising

  3. David Lozzi

    Alex Meleta Guest

    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
     
    Alex Meleta, Jul 10, 2007
    #3
  4. David Lozzi

    vMike Guest

    "David Lozzi" <> wrote in message
    news:D...
    > 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
     
    vMike, Jul 10, 2007
    #4
  5. David Lozzi

    David Lozzi Guest

    Man, I keep forgetting about that little tool. I hope it's available for
    Vista!


    "Alex Meleta" <> wrote in message
    news:...
    > Hi David,
    >
    >> Then I attach this file to an email and send it off.

    >
    > As suggestion: try to obtain what locks your file (e.g. by using process
    > explorer http://www.sysinternals.com/ntw2k/freeware/procexp.shtml). May be
    > mailer has not been freeing it.
    >
    > Regards, Alex Meleta
    > [TechBlog] http://devkids.blogspot.com
    >
    >
     
    David Lozzi, Jul 10, 2007
    #5
  6. David Lozzi

    David Lozzi Guest

    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" <> wrote in message
    news:ZZNki.20972$...
    >
    > "David Lozzi" <> wrote in message
    > news:D...
    >> 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
    >
    >
     
    David Lozzi, Jul 10, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page