作为附件发送后文件被锁定
我正在将文件作为附件发送:
// 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
处理您的
消息
将为您解决此问题。尝试在移动文件之前对消息调用Dispose
,如下所示:处置 MailMessage 时,所有锁和资源都会被释放。
Disposing your
message
will fix this for you. Try callingDispose
on your message before moving the file, like so:When disposing MailMessage, all locks and resources are released.
您需要利用“using”关键字:
这是因为“using”确保在代码块执行结束时调用 IDisposable.Dispose 方法。
每当您使用某个管理某些资源的类时,请检查它是否是 IDisposable,然后使用“using”块,这样您就不需要关心手动处置调用 IDisposable.Dispose。
You need to take advantage of "using" keyword:
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.
附件是IDisposable 并应在发送后正确处置以释放文件上的锁定
Attachments are IDisposable and should be disposed of correctly after they have been sent to release the lock on the file
为了防止发生此文件锁定,您可以使用
using
因为这将自动释放该对象:In order to prevent this file lock from happening, you could use
using
as this will dispose of the object automatically:使用完附件后,还有另一种处理附件的方法...
Here's another way to dispose of the Attachments, once you've finished with them...
一个共享的类似问题。我必须在给定图像上使用 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.