This project has moved. For the latest updates, please go here.

Read .pst or .ost file

Mar 7, 2013 at 11:44 PM
I need to be able to search through .pst and .ost files that are stored on a server directory and not part of my default profile. I've used the following code:
            Outlook.NameSpace outlookNS = (Outlook.NameSpace)outlookApp.GetNamespace("MAPI");
            outlookNS.AddStore(@"C:\Temp\SearchMe\test.pst");
            Outlook.MAPIFolder rootFolder = outlookNS.Stores["test.pst"].GetRootFolder();
            foreach (COMObject item in rootFolder.Items)
            {
                Outlook.MailItem mailItem = item as Outlook.MailItem;
                if (mailItem != null)
                {
                    System.Diagnostics.Debug.Print(mailItem.Body);
                }
            }
There are 3 problems that I'm trying to address:
  1. This uses the default profile on my computer. I'd prefer not to do this. Is there a way to create a temporary profile?
  2. The line "Outlook.MAPIFolder rootFolder = outlookNS.Stores["test.pst"].GetRootFolder();" throws an exception. I can get around this by referring to the store by its ordinal number but that is a little messy. How do I properly refer to the store I've just added?
  3. There is no apparent Namespace.RemoveStore() method in the NetOffice API. How do I remove a store once I'm done with it?
Thanks in advance for any help.
Developer
Mar 8, 2013 at 9:28 AM
RussSuter wrote:
The line "Outlook.MAPIFolder rootFolder = outlookNS.Stores["test.pst"].GetRootFolder();" throws an exception. I can get around this by referring to the store by its ordinal number but that is a little messy. How do I properly refer to the store I've just added?
An exception is thrown because of incorrect value ("test.pst") for Stores indexer. You should pass store DisplayName but it may be unknown at runtime. So I propose you to get store by it's path.
 Outlook.Store GetStoreByPath(string path)
  {
      foreach (Outlook.Store store in ns.Stores)
      {
        if (store.FilePath.Equals(path, StringComparison.InvariantCulture))
          return store;
      }
      return null;
 }

void AddStore()
{
    const string pstPath = (@"C:\Temp\SearchMe\test.pst");
    ns.AddStore(pstPath);
    var storeRoot = GetStoreByPath(pstPath).GetRootFolder();
}
Mar 11, 2013 at 10:14 PM
Edited Mar 11, 2013 at 10:20 PM
Thanks. That takes care of the second issue. Anyone have any input on the other two?
Sep 26, 2014 at 8:51 AM
Hi guys!

I am also looking for RemoveStore() method. Did you find something?

With regards, Viljem