无法移动文件:该进程无法访问该文件,因为该文件正在被另一个进程使用
场景:我编写了一个应用程序来打开 .msg 文件列表(已转储到文件系统),从中获取一些信息(主题、抄送),然后移动它们。
问题:但是,当涉及到移动文件时,我收到以下错误:
该进程无法访问该文件,因为该文件正在被使用 另一个进程。
对文件运行 Handle 仅显示我编写的工具,而不显示其他句柄。
因此,我认为当我完成将文件用作 Redemption MessageItem 对象时,我没有正确释放这些文件。
但我无法将它们包装在 using 语句中,因为它们没有实现 IDisposable。而且它们不会公开任何公共 Close 或 Dispose 或类似名称的方法。
简而言之,我想问:
a)如何强制我的 C# 应用程序关闭给定的句柄,只知道文件句柄的路径?
或者
b) 有没有办法强制关闭 Redemption 对象?
var util = new MAPIUtilsClass();
MessageItem item = util.GetItemFromMsgFile(EmailPath, false);
item.Import(EmailPath, 3);
Subject = item.Subject;
From = (item.SenderName.Length < 96) ? item.SenderName : item.SenderName.Substring(0, 93) + "...";
To = (String.IsNullOrEmpty(item.To)) ? String.Empty : (item.To.Length < 96) ? item.To : item.To.Substring(0, 93) + "...";
CC = (String.IsNullOrEmpty(item.CC)) ? String.Empty : (item.CC.Length < 96) ? item.CC : item.CC.Substring(0, 93) + "...";
Sent = item.SentOn;
Received = item.ReceivedTime;
Log.Write("Redemption: Email data harvested" + EmailPath);
Scenario: I've written an application to open a list of .msg files (which have been dumped to the file system), grab some information from them (subject, To CC) and then move them.
Problem: However, when it comes to moving the file I get the following error:
The process cannot access the file because it is being used by
another process.
Running Handle against the file only shows the tool I've written and no other handles.
I assume, therefore, that I'm not properly releasing the files when I've finished using them as Redemption MessageItem objects.
But I can't wrap them in a using statement, because they don't implement IDisposable. And they don't expose any public Close or Dispose or similarly named methods.
In short, I'm trying to ask:
a) How can I force my c# application to close a given handle, knowing only the path to the file handle?
Or
b) Is there a way to force the Redemption objects to close?
var util = new MAPIUtilsClass();
MessageItem item = util.GetItemFromMsgFile(EmailPath, false);
item.Import(EmailPath, 3);
Subject = item.Subject;
From = (item.SenderName.Length < 96) ? item.SenderName : item.SenderName.Substring(0, 93) + "...";
To = (String.IsNullOrEmpty(item.To)) ? String.Empty : (item.To.Length < 96) ? item.To : item.To.Substring(0, 93) + "...";
CC = (String.IsNullOrEmpty(item.CC)) ? String.Empty : (item.CC.Length < 96) ? item.CC : item.CC.Substring(0, 93) + "...";
Sent = item.SentOn;
Received = item.ReceivedTime;
Log.Write("Redemption: Email data harvested" + EmailPath);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
完成后尝试调用 util.CleanUp 。
Try calling
util.CleanUp
after you are done.不要使用
MAPIUtils.GetItemFromMsgFile
- 它已被弃用。使用
RDOSession.GetMessageFromMsgFile
(或CreateMessageFromMsgFile
) - 它返回支持IDisposable
的IRDOMail
对象。Do not use
MAPIUtils.GetItemFromMsgFile
- it is deprecated.Use
RDOSession.GetMessageFromMsgFile
(orCreateMessageFromMsgFile
) - it returnsIRDOMail
object which does supportIDisposable
.按照 Daniel 的建议使用 Util.CleanUp() 方法,但立即使用 GC.Collect() 来成功,并将其放在 try-catch 的 finally 语句中。
所以代码现在看起来像这样:
Use Util.CleanUp() method as suggested by Daniel, but succeed it immediately with GC.Collect() and put it within a finally statement of a try-catch.
So the code now looks like this: