Skip to main content
 首页 » 编程设计

c#之在 c# 中从 outlook 添加文件夹到 pst 文件

2024年02月23日17bonelee

我一直在努力寻找一种方法来将文件夹添加到 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() 获取该商店的根文件夹 的句柄(不是 Store . session )。所以你会使用:

// create a folder or subfolder in pst     
Outlook.MAPIFolder pstRootFolder = NewPst.GetRootFolder(); 
Outlook.MAPIFolder NewFolder = pstRootFolder.Folders.Add("New Test Folder"); 

我建议为以下两项添加书签:PIA 文档并不总是完整的,因此值得查看 COM 文档以及完整的类和成员信息。