I am trying to add a feature to a website that will send an outlook invite attachment to email. Everything runs the first time perfectly, but after running it again, I get the following error: "The process cannot access the file 'OutlookCalendar.ics' because it is being used by another process." I have searched high and low for an answer to this but it appears that when I use file.WriteAllLines, it doesn't close the file? See code below...
//CREATE OUTLOOK ATTACHMENT.....................................................................................................
//..............................................................................................................................
string schLocation = "Kettle Cuisine TBD";
string schSubject = visitor + " is visiting.";
string schDescription = other;
System.DateTime schBeginDate = Convert.ToDateTime(date + " " + time);
System.DateTime schEndDate = Convert.ToDateTime(date + " 17:00");
//PUTTING THE MEETING DETAILS INTO AN ARRAY OF STRING
String[] contents = { "BEGIN:VCALENDAR",
"PRODID:-//Flo Inc.//FloSoft//EN",
"BEGIN:VEVENT",
"DTSTART:" + schBeginDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"),
"DTEND:" + schEndDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"),
"LOCATION:" + schLocation,
"DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + schDescription,
"SUMMARY:" + schSubject, "PRIORITY:3",
"END:VEVENT", "END:VCALENDAR" };
/*THE METHOD 'WriteAllLines' CREATES A FILE IN THE SPECIFIED PATH WITH
THE SPECIFIED NAME,WRITES THE ARRAY OF CONTENTS INTO THE FILE AND CLOSES THE
FILE.SUPPOSE THE FILE ALREADY EXISTS IN THE SPECIFIED LOCATION,THE CONTENTS
IN THE FILE ARE OVERWRITTEN*/
System.IO.File.WriteAllLines(Server.MapPath("OutlookCalendar.ics"), contents);
//END CREATE OUTLOOK ATTACHMENT.....................................................................................................
//..............................................................................................................................
//Send out email
Email.sendEmailOutlook(message, email, header);
In the email section I use this.... //MAKE AN ATTACHMENT OUT OF THE .ICS FILE CREATED
Attachment mailAttachment = new Attachment(System.Web.HttpContext.Current.Server.MapPath("OutlookCalendar.ics"));
mail.Attachments.Add(mailAttachment);
mail.IsBodyHtml = true;
//Set email message and subjet
mail.Subject = header;
mail.Body = message;
smtpClient.Send(mail);
I suspect the problem isn't with File.WriteAllLines
, but with Attachment
- that implements IDisposable
, but you're not disposing of it... so if that's got an open file handle on it, that would explain what you're seeing. You can just use:
var path = HttpContext.Current.Server.MapPath("OutlookCalendar.ics");
using (Attachment mailAttachment = new Attachment(path))
{
mail.Attachments.Add(mailAttachment);
mail.IsBodyHtml = true;
//Set email message and subjet
mail.Subject = header;
mail.Body = message;
smtpClient.Send(mail);
}
However, it's not clear why you need to write to a file at all. It would be better to change your code to just create a MemoryStream
. For example:
string text = string.Join("\r\n", contents);
byte[] encoded = Encoding.UTF8.GetBytes(text);
var stream = new MemoryStream(encoded);
using (var attachment = new Attachment(stream, "OutlookCalendar.ics"))
{
...
}
Now no files are involved at all, so there can't be any conflicts.
See more on this question at Stackoverflow