作为附件发送后文件被锁定

发布于 2024-10-20 06:36:47 字数 1121 浏览 11 评论 0原文

我正在将文件作为附件发送:

            // 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)
                    {
                    }

它会抛出一个异常,表明该文件已在另一个进程中使用。如何解锁该文件以便将其移动到该位置?

I am sending a file as an attachment:

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

And then I want to move the file to another folder, however when I try to do this

                    try
                    {
                        //File.Open(oldFullPath, FileMode.Open, FileAccess.ReadWrite,FileShare.ReadWrite);
                        File.Move(oldFullPath, newFullPath);

                    }
                    catch (Exception ex)
                    {
                    }

Its throwing an exception that the file is already being used in another process. How I can unlock this file so that it can be moved to this location?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

迷离° 2024-10-27 06:36:47

处理您的消息将为您解决此问题。尝试在移动文件之前对消息调用 Dispose,如下所示:

message.Dispose();
File.Move(...)

处置 MailMessage 时,所有锁和资源都会被释放。

Disposing your message will fix this for you. Try calling Dispose on your message before moving the file, like so:

message.Dispose();
File.Move(...)

When disposing MailMessage, all locks and resources are released.

夏有森光若流苏 2024-10-27 06:36:47

您需要利用“using”关键字:

using(Attachment att = new Attachment(...))
{
   ...
}

这是因为“using”确保在代码块执行结束时调用 IDisposable.Dispose 方法。

每当您使用某个管理某些资源的类时,请检查它是否是 IDisposable,然后使用“using”块,这样您就不需要关心手动处置调用 IDisposable.Dispose。

You need to take advantage of "using" keyword:

using(Attachment att = new Attachment(...))
{
   ...
}

That's because "using" ensures IDisposable.Dispose method is called at the end of code block execution.

Whenever you use some class that's managing some resource, check if it's IDisposable and then use "using" block and you won't need to care about manually disposing calling IDisposable.Dispose.

瞄了个咪的 2024-10-27 06:36:47

附件IDisposable 并应在发送后正确处置以释放文件上的锁定

Attachments are IDisposable and should be disposed of correctly after they have been sent to release the lock on the file

帅的被狗咬 2024-10-27 06:36:47

为了防止发生此文件锁定,您可以使用 using 因为这将自动释放该对象:

using (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); 
    // Add your send code in here too
}

In order to prevent this file lock from happening, you could use using as this will dispose of the object automatically:

using (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); 
    // Add your send code in here too
}
羁〃客ぐ 2024-10-27 06:36:47

使用完附件后,还有另一种处理附件的方法...

//  Prepare the MailMessage "mail" to get sent out...
await Task.Run(() => smtp.Send(mail));

//  Then dispose of the Attachments.
foreach (Attachment attach in mail.Attachments)
{
    //  Very important, otherwise the files remain "locked", so can't be deleted
    attach.Dispose();
}

Here's another way to dispose of the Attachments, once you've finished with them...

//  Prepare the MailMessage "mail" to get sent out...
await Task.Run(() => smtp.Send(mail));

//  Then dispose of the Attachments.
foreach (Attachment attach in mail.Attachments)
{
    //  Very important, otherwise the files remain "locked", so can't be deleted
    attach.Dispose();
}
坠似风落 2024-10-27 06:36:47

一个共享的类似问题。我必须在给定图像上使用 image.dispose(),然后才能对创建图像的文件执行某些操作。

A shared similar problem. I had to use image.dispose() on a given image before I could do something with the file that the image was created from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文