重命名Outlook 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);
}

但没有成功。我总是得到一个例外,那就是我没有更改名称的权限。其他自定义创建的文件夹,我可以重命名没有任何问题。

有什么事情可以解锁文件夹吗?或者还有其他访问文件夹的可能性吗?

非常感谢

编辑:费用是:您没有权限。

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);
            }
        }
    }

当我在管理员模式下运行visualstudio时,这段代码对我有效。。

最新更新