Search code examples
c#.netoutlookoutlook-redemption

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

Solution

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