重命名Outlook PST文件中的文件夹

时间:2012-03-07 09:11:13

标签: c# outlook outlook-2007 outlook-2010 pst

几天后,我尝试通过c#重命名已​​发送邮件文件夹,已删除元素和收件箱文件夹。

我试过这样的事情:

 List<Outlook.MailItem> mailItems = new List<Outlook.MailItem>();
            Outlook.Application app = new Outlook.Application();
            Outlook.NameSpace outlookNs = app.GetNamespace("MAPI");
            // Add PST file (Outlook Data File) to Default Profile
            outlookNs.AddStore(pstFilePath);
            Outlook.MAPIFolder rootFolder = outlookNs.Stores[pstName].GetRootFolder();

           Outlook.Folders subFolders = rootFolder.Folders;

  foreach (Outlook.Folder folder in subFolders)
            {

              folder.Name =  (folder.Name == "deleted Elements"?"deleted":folder.Name);
}

但没有成功。我总是得到我无权更改名称的例外情况。其他自定义创建的文件夹我可以毫无问题地重命名。

解锁文件夹有什么办法吗? 或者是否有其他可能访问文件夹?

非常感谢

编辑:Expetion是:您没有权限。

1 个答案:

答案 0 :(得分:1)

public string RenameFolder(string name, string folderid)
    {
        Outlook.Application app = new Outlook.Application();
        Outlook.NameSpace ns = null;
        Outlook.Folder folder = null;
        string n= null;

        try
        {
            ns = app.GetNamespace("MAPI");
            folder = ns.GetFolderFromID(folderid) as Outlook.Folder;
            n=folder.Name;
            folder.Name = (folder.Name = name) ;
            return n + " has been successfully changed to " + folder.Name;
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (app != null)
            {
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(app);
            }

            if (folder != null)
            {
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(folder);
            }

            if (ns != null)
            {
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(ns);
            }
        }
    }

当我在管理员模式下运行visual studio时,这段代码对我有用..