开发者

Unable to move file: The process cannot access the file because it is being used by another process

Scenario: I've written an application to open a list of .msg files (which have been dumped to the file system), grab some info开发者_高级运维rmation 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);


Try calling util.CleanUp after you are done.


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


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();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜