I am working on a win app which accesses the Outlook personal folders. Internally, it mounts the personal folder on the Outlook instance and processes the mails and then unmounts the pst. After unmounting the pst, I delete that file.
Now the problem is that even after un mounting the pst and releasing memory, when I try to delete the pst, I get an exception that some process is using this file so it can not be deleted. And that process is outlook. I am using following code to un mount and release memory:
_application.Session.RemoveStore(_personalFolder);
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(_personalFolder) >= 0) ;
_personalFolder = null;
I checked it many times and it seems as if it takes some time to release memory and there is no definite time for this. Could any one help me how to delete the file?
Have a look at KB234228 ("OL97: Outlook .pst File Lock Release Interval Now Configurable"). It is about Outlook 97, but I would guess that it still works.
I guess that FileSystemWatcher will not help you, since there is no "exclusive lock released" event.
So maybe you can make a worker thread, that continuously tries to delete all the .pst files you put in a queue. This way your application UI can at least go on uninterrupted.
Tools like ProcMon do have the ability to look at the file handles of applications. Maybe you are able to make an API call that allows you doing that for the Oulook process (That's good material for another SO question, I guess). This way you don't have to poll the file system, and can act more quickly when the lock is gone.
If you dare, you might even try and close the file handle in question right away, removing the need to poll/wait altogether.