C# 在c中从outlook向pst文件添加文件夹#

C# 在c中从outlook向pst文件添加文件夹#,c#,.net,outlook,C#,.net,Outlook,我一直在试图找到一种方法,从c向pst文件添加一个文件夹# 我已经尝试了一大堆代码,试图让它工作,这是一个似乎是最有可能是正确的(因为它是什么在MSDN上),但仍然不工作 Main { Outlook._Application OutlookObject = new Outlook.Application(); Outlook.Store NewPst = null; // create the pst file strin

我一直在试图找到一种方法,从c向pst文件添加一个文件夹#

我已经尝试了一大堆代码,试图让它工作,这是一个似乎是最有可能是正确的(因为它是什么在MSDN上),但仍然不工作

Main {

Outlook._Application OutlookObject = new Outlook.Application();
            Outlook.Store NewPst = null;
           // create the pst file
            string pstlocation = "C:\\Users\\Test\\Desktop\\PST\\Test.pst";
            try
        {
            OutlookObject.Session.AddStore(pstlocation);

            foreach (Outlook.Store store in OutlookObject.Session.Stores)
            {
                if (store.FilePath == pstlocation)
                {
                    // now have a referance to the new pst file
                    NewPst = store;
                    Console.WriteLine("The Pst has been created");
                }
            }
        }
        catch
        { }
        // create a folder or subfoler in pst
        Outlook.MAPIFolder NewFolder;

        NewFolder = NewPst.Session.Folders.Add("New Test Folder", Type.Missing);
}
此代码创建一个新的PST文件,然后尝试向其中添加文件夹,但最后一行代码:

New NewFolder = NewPst.Session.Folders.Add("New Test Folder", Type.Missing);
获取错误“操作失败”和“无效强制转换异常”,有人能指出我做错了什么吗


提前感谢

您需要使用
Store.GetRootFolder()
来获取该存储的根文件夹的句柄(不是
)。因此,您可以使用:

// create a folder or subfolder in pst    
Outlook.MAPIFolder pstRootFolder = NewPst.GetRootFolder();
Outlook.MAPIFolder NewFolder = pstRootFolder.Folders.Add("New Test Folder");
我建议将以下两项都添加到书签中:PIA文档并不总是完整的,因此值得查看COM文档以获取完整的类和成员信息

  • (.NET):
  • (COM):

您需要使用
Store.GetRootFolder()
来获取该存储的根文件夹的句柄(不是
)。因此,您可以使用:

// create a folder or subfolder in pst    
Outlook.MAPIFolder pstRootFolder = NewPst.GetRootFolder();
Outlook.MAPIFolder NewFolder = pstRootFolder.Folders.Add("New Test Folder");
我建议将以下两项都添加到书签中:PIA文档并不总是完整的,因此值得查看COM文档以获取完整的类和成员信息

  • (.NET):
  • (COM):