无法移动文件:该进程无法访问该文件,因为该文件正在被另一个进程使用

发布于 2024-11-19 23:22:41 字数 1164 浏览 1 评论 0原文

场景:我编写了一个应用程序来打开 .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 技术交流群。

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

发布评论

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

评论(3

月亮是我掰弯的 2024-11-26 23:22:41

完成后尝试调用 util.CleanUp 。

Try calling util.CleanUp after you are done.

流年已逝 2024-11-26 23:22:41

不要使用MAPIUtils.GetItemFromMsgFile - 它已被弃用。
使用RDOSession.GetMessageFromMsgFile(或CreateMessageFromMsgFile) - 它返回支持IDisposableIRDOMail对象。

Do not use MAPIUtils.GetItemFromMsgFile - it is deprecated.
Use RDOSession.GetMessageFromMsgFile (or CreateMessageFromMsgFile) - it returns IRDOMail object which does support IDisposable.

马蹄踏│碎落叶 2024-11-26 23:22:41

按照 Daniel 的建议使用 Util.CleanUp() 方法,但立即使用 GC.Collect() 来成功,并将其放在 try-catch 的 finally 语句中。

所以代码现在看起来像这样:

    MAPIUtilsClass util = null;

    try
    {

        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;

        util.Cleanup();

        Log.Write("Redemption: Email data harvested: " + EmailPath);


    }
    catch (Exception exp)
    {
        Log.Write(String.Format("Error using Redemption API against: {0}\r\n{1}\r\n{2}",
            this.EmailPath, exp.Message, exp.StackTrace));
    }

    finally
    {
        if (util != null)
            util.Cleanup();

        GC.Collect();
    }

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:

    MAPIUtilsClass util = null;

    try
    {

        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;

        util.Cleanup();

        Log.Write("Redemption: Email data harvested: " + EmailPath);


    }
    catch (Exception exp)
    {
        Log.Write(String.Format("Error using Redemption API against: {0}\r\n{1}\r\n{2}",
            this.EmailPath, exp.Message, exp.StackTrace));
    }

    finally
    {
        if (util != null)
            util.Cleanup();

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