HW90
HW90

Reputation: 1983

Rename Folders in Outlook PST-File

since a few days I'm trying to rename the sent mail folder, deleted elements and the inbox folder via c#.

I've tryed something like this:

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

But without success. I always get the exceptiion that I do not have permissions to change the name. Other custom created folders I'm able to rename without any problems.

Is there something to do to unlock the folder? Or is there an other possibility to access the folders?

Thanks a lot

Edit: The Expetion is: You do not have permissions.

Upvotes: 1

Views: 2021

Answers (1)

Hussnain Hashmi
Hussnain Hashmi

Reputation: 111

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

this code is working for me..when i run visual studio in administator mode..

Upvotes: 1

Related Questions