Hi -- I am trying to create a .msg file using Outlook.Application. I
save it on disk and once I am done working with it, I want to delete
the file. But it seems even after I set it to null and open the file
using FileInfo or FileStream to delete it, I cant, is there someone
who can direct why Outlook may be holding a handling to it?
Oh I can delete the same file if I go to explorer and press delete. ??
If anyone needs that piece of code, here it is
This part creates the file and save it.
string DefinedPath = Environment.GetFolderPath
(Environment.SpecialFolder.ProgramFiles) + "\\XYZ\\" + FileName ;
Outlook.Application objOutlook = new
Outlook.Application();
Outlook.MailItem objMailItem = (Outlook.MailItem)
(objOutlook.CreateItem(Outlook.OlItemType.olMailItem));
objMailItem.To = "someemailaddress";
objMailItem.Subject = "XYZ";
objMailItem.Importance =
Outlook.OlImportance.olImportanceHigh;
objMailItem.HTMLBody = data;
objMailItem.SaveAs(DefinedPath,
Outlook.OlSaveAsType.olMSG);
objMailItem = null;
This is where I attach the file to another msg that I send out.
System.Net.Mail.Attachment AttachData = new System.Net.Mail.Attachment
(DefinedPath, MediaTypeNames.Application.Octet);
msg.Attachments.Add(AttachData);
Now the deletion part I get an exception when File.Delete is being
executed.
FileStream fs = File.Open(DefinedPath, FileMode.Open,
FileAccess.Write, FileShare.None);
fs.Close();
File.Delete(DefinedPath);
Is there something I am doing wrong ??
Thanks,
S