Why, after this code executes, can I not manually delete the generated file? I get this error message:
The action can't be completed because the file is open in IIS Express Workers Process.
The code:
System.IO.File.AppendAllText("Filename.txt", "Some Text");
The application is a web application and running in IIS. My understanding was AppendAllText
automatically closes the file handle.
I am not using this code to log. The purpose of the code is to create a CSV file that is then attached to an email. If I execute the code and email the file, the file is never sent. If I then restart IIS (to release the lock) and execute only the code to mail the file (no generation), it works.
The code to attach the file...
SmtpClient mailClient = new SmtpClient();
MailMessage msg = new MailMessage();
... Add From, Subject, Body, etc... here
msg.Attachments.Add(new Attachment(filename));
mailClient.Send(msg);
No exceptions are thrown, nothing is written to the log file indicating problems. The email is simply not sent and if I go to the generated file and attempt to delete it, I cannot as IIS has it locked. I believe the lock is why the email is never sent.