我遇到错误
文件正在被另一个进程使用
尝试为using
实现FileStream
。但是,我遇到了Stream was not readable
错误。
这是我的代码:
之前:可以正常工作,但是会定期遇到“文件正在被另一个进程使用”错误
EmailMessage responseMessageWithAttachment = responseMessage.Save();
foreach (var attachment in email.Attachments)
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
fileAttachment.Load();
fileAttachment.Load(AppConfig.EmailSaveFilePath + fileAttachment.Name);
FileStream fs = new FileStream(AppConfig.EmailSaveFilePath + fileAttachment.Name,FileMode.OpenOrCreate);
responseMessageWithAttachment.Attachments.AddFileAttachment(attachment.Name,fs);
}
}
responseMessageWithAttachment.SendAndSaveCopy();
之后:遇到“流不可读”错误
EmailMessage responseMessageWithAttachment = responseMessage.Save();
foreach (var attachment in email.Attachments)
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
fileAttachment.Load();
fileAttachment.Load(AppConfig.EmailSaveFilePath + fileAttachment.Name);
using (FileStream fs = new FileStream(AppConfig.EmailSaveFilePath + fileAttachment.Name,FileMode.OpenOrCreate))
{
responseMessageWithAttachment.Attachments.AddFileAttachment(attachment.Name,fs);
};
}
}
responseMessageWithAttachment.SendAndSaveCopy();