我正在发送一个文件作为附件:
// Create the file attachment for this e-mail message.
Attachment data = new Attachment(filePath, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(filePath);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(filePath);
disposition.ReadDate = System.IO.File.GetLastAccessTime(filePath);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);
然后我想将文件移动到另一个文件夹,但是当我尝试这样做时
try
{
//File.Open(oldFullPath, FileMode.Open, FileAccess.ReadWrite,FileShare.ReadWrite);
File.Move(oldFullPath, newFullPath);
}
catch (Exception ex)
{
}
它抛出一个异常,表明该文件已在另一个进程中使用。如何解锁此文件以便将其移动到此位置?
最佳答案
处理您的消息
将为您解决这个问题。尝试在移动文件之前对消息调用 Dispose
,如下所示:
message.Dispose();
File.Move(...)
在处理 MailMessage 时,所有的锁和资源都被释放。
关于c# - 文件作为附件发送后被锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5191449/