2012-03-07 79 views
1

因为几天我试图通过c#重命名发送的邮件文件夹,删除的元素和收件箱文件夹。重命名Outlook中的文件夹PST文件

我tryed是这样的:

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是:您没有权限。

+0

请提供有关异常的文档。你的布尔赋值语句没有什么意义。 – 2012-03-07 12:44:07

+0

我在Outlook 2010中寻找相同的选项,但没有运气。显然这没有选择。据我了解,较早版本的Outlook支持重命名默认文件夹。我会欢迎任何想法。 – 2014-04-14 21:09:53

回答

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

此代码工作me..when我运行administator模式的Visual Studio ..

+0

您能否介绍一下双文件夹重命名的必要性? 'folder.Name =(folder.Name = name);' – 2018-01-15 11:28:15