Results 1 to 4 of 4

Thread: Attachment class combined with FileInfo causing issues

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    9

    Attachment class combined with FileInfo causing issues

    OK I am working on a windows service that monitors specific directories and checks to see if a file is changed. If a file is changed, it is attached to an email and sent to an admin.

    The way I solve the problem...
    I create a FileInfo object to do the following:
    -I check to see if the file has an extension that is allowed to be attached
    -I check to see if the file is under a max size to be attached

    I create an Attachment object to do the following:
    -attach the file to an email

    I then send the email.

    The problem with this is when I edit the file after it is successfully attached the first time, I CANNOT save changes to that file. I get an error message saying that it is being used and cannot change it. My solution to this problem is to call the "dispose()" method of the attachment class. When i do this, I can edit the file all I want to and the first attachment works but after that the "dispose()" method always throws an exception and the file is never attached to the email when the file is edited again. Why is this happening?

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Attachment class combined with FileInfo causing issues

    Perhaps post some example code, and what type of exception is it throwing?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Attachment class combined with FileInfo causing issues

    When you create an Attachment object, it opens the file and locks it. It does this specifically so that you CAN'T change the file. If you want to change the file then you must dispose the Attachment. Once disposed, the Attachment is unusable. That's the way it is supposed to be. If you want to then edit the file and attach it again, you must create a new Attachment object. That's the way it's supposed to be.

    Ideally, you should be using the Using statement for Attachments and pretty much all other short-lived disposable objects. That way you clearly delimit the lifetime of the object, both for yourself and the system, e.g.
    vb.net Code:
    1. Using attachedFile As New Attachement(file, MediaTypeNames.Application.Octet)
    2.     'Use attachedFile here
    3. End Using
    The variable 'attachedFile' doesn't exist outside the Using block so you can't even try to use it. Also, the object is created at the Using statement and then, most importantly, guaranteed to be disposed at the End Using statement, no matter how you get there. The object will even be disposed if an unhandled exception occurs.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    9

    Re: Attachment class combined with FileInfo causing issues

    The using statement did the trick! Thanks a lot. I tried using the "dispose()" method but it caused a crash. I was thinking that the attachment object would go out of scope and that would cause it to automatically dispose. Apparently I was wrong. Thanks again for your help!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width