|
-
Oct 29th, 2010, 11:44 AM
#1
Thread Starter
New Member
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?
-
Oct 29th, 2010, 05:53 PM
#2
Re: Attachment class combined with FileInfo causing issues
Perhaps post some example code, and what type of exception is it throwing?
-
Oct 29th, 2010, 08:39 PM
#3
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:
Using attachedFile As New Attachement(file, MediaTypeNames.Application.Octet) 'Use attachedFile here 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.
-
Nov 1st, 2010, 01:32 PM
#4
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|