c# - 文件作为附件发送后被锁定

标签 c# attachment locked-files

我正在发送一个文件作为附件:

            // 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/

相关文章:

windows - Microsoft VSHADOW.EXE 卷影副本包含锁定的文件?

c# - 如何为 Web 服务生成 XSD 文件

c# - 获取 WPF 项目的项目名称

C# Diamond-Inheritance(接口(interface)实现)

c# - 如何使用泛型参数为任何方法构建表达式调用

php - Swift Mailer 附件

ruby-on-rails - 如何使用 Paperclip 为我的附件指定文件名?

Azure DevOps : How to enable the Rename locked files (RenameFilesFlag) with an "IIS web app deploy" task