I recently came across a situation where the Dispose method had to be hard-coded in a C# program. Otherwise the file used in an email will be "forever" locked and not even Process Manager was able to tell me who/what locked it. I had to use Unlocker Assistant to force delete the file, but I fear now I've left some allocated memory blocks on the server.
The code I am referring to is this:
MailMessage mail = new MailMessage();
mail.From = new MailAddress("reception@domain.com", "###");
mail.Subject = "Workplace Feedback Form";
Attachment file = new Attachment(uniqueFileName);
mail.Attachments.Add(file);
mail.IsBodyHtml = true;
mail.CC.Add("somebody@domain.com");
mail.Body = "Please open the attached Workplace Feedback form....";
//send it
SendMail(mail, fldEmail.ToString());
The above code left the file from uniqueFileName
locked by the Attachment handle and I was not able to delete it and because this code ran from a client machine (not from the server itself), the handle to the file was impossible to find.
After I had forced a deletion the files, I found from another forum that I should have Disposed of the Attachment object.
So I added these lines of code after the email was sent ...
//dispose of the attachment handle to the file for emailing,
//otherwise it won't allow the next line to work.
file.Dispose();
mail.Dispose(); //dispose of the email object itself, but not necessary really
File.Delete(uniqueFileName); //delete the file
Should I have wrapped this in a using
statement instead?
And that's the crux of my question. When should we use Using and when should we use Dispose? I hope there is a clear distinction between the two which says if you do "X" then use this, else use that.
This When to Dispose? and this C# Dispose : when dispose and who dispose it do answer my question somewhat, but I am still confused about the "conditions" on when to use either.