我目前正在调试我的代码,因为它给了我一个错误:
The process cannot access the file because it is being used by another process.
我认为错误发生在这行代码中
foreach (var filename in filenames)
{
var file = Path.Combine(filePath, filename);
mail.Attachments.Add(new Attachment(file));
}
// Send Mail
smtpServer.Send(mail);
DeleteFiles();
我想在使用此方法发送邮件时删除文件夹中的文件
private void DeleteFiles()
{
string filePath = Server.MapPath("~/Content/attachments");
Array.ForEach(Directory.GetFiles(filePath), System.IO.File.Delete);
}
我读到关于关闭/处置?FileStream 等,但我如何在我的代码中使用它?提前致谢。