我正在使用下面的代码在内容的本地驱动器中创建一个文件。该进程无法访问该文件,因为它正在被另一个进程使用
File.WriteAllLines(path, contents);
我将此文件附加到邮件并发送给团队。一旦邮件发送我需要删除的文件,删除我使用下面的代码的文件,但我得到的运行时错误
File.Delete(path);
错误消息:该进程无法访问该文件,因为它正在使用另一个过程
默认情况下,WriteAllLines()方法关闭文件,但它仍然由另一个进程打开。我只能在某段时间之后通过执行代码来删除文件,但这不是这种情况。邮件发送后我需要删除它。
更新
System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
mailMessage.To.Add(new System.Net.Mail.MailAddress(recipient, ToName));
mailMessage.From = new System.Net.Mail.MailAddress(From, FromName);
mailMessage.Subject = Subject; // "Outlook calendar as attachment"; // modified by Srikanth J on 28/06/2012
mailMessage.Body = "This is a test message";
System.Net.WebClient webclient = new System.Net.WebClient();
webclient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
for (int i = 0; i < item.Attachments.Count; i++)
{
string url = item.Attachments.UrlPrefix + item.Attachments[i];
SPFile file = item.ParentList.ParentWeb.GetFile(url);
mailMessage.Attachments.Add(new System.Net.Mail.Attachment(file.OpenBinaryStream(), file.Name));
}
System.Net.Mail.Attachment mailAttachment = new System.Net.Mail.Attachment(path);
mailMessage.Attachments.Add(mailAttachment);
smtp.Send(mailMessage);
任何帮助appriciated,谢谢。
似乎将文件附加到邮件的过程仍然保存文件? –
你可以发布附加文件的代码吗? @CuongLe建议,这很可能是问题所在。 –
@CuongLe你的意思是即使在发送邮件之后,它仍然由邮件进程持有? – Srikanth