Get Folder Information from PST File in C#, VB.NET

The PST files are used to store information that pertains to the e-mail folders, addresses, contact information, email messages and other data that is saved within Outlook and Exchange programs. Spire.Email supports to read PST files and get the folder information such as folder name, message count and unread message count.

Step 1: Load a PST file from disk into an instance of OutlookFile class.

OutlookFile olf = new OutlookFile(@"C:\Users\jack\Documents\Outlook Files\Sample.pst");

Step 2: Get the folders collection.

OutlookFolderCollection folderCollection = olf.RootOutlookFolder.GetSubFolders();

Step 3: Traverse the collection and get the folder information of each element in the collection.

foreach (OutlookFolder folder in folderCollection)
{
    Console.WriteLine("Folder: " + folder.Name);
    Console.WriteLine("Total items: " + folder.ItemCount);
    Console.WriteLine("Total unread items: " + folder.UnreadItemCount);
    Console.WriteLine("Whether this folder has subfolders: {0}", (folder.HasSubFolders)?"Yes":"No");
    Console.WriteLine("------------------Next Folder--------------------");
}

Output:

Get Folder Information from PST File in C#, VB.NET

Full Code:

[C#]
OutlookFile olf = new OutlookFile(@"C:\Users\jack\Documents\Outlook Files\Sample.pst");
OutlookFolderCollection folderCollection = olf.RootOutlookFolder.GetSubFolders();

foreach (OutlookFolder folder in folderCollection)
{
    Console.WriteLine("Folder: " + folder.Name);
    Console.WriteLine("Total items: " + folder.ItemCount);
    Console.WriteLine("Total unread items: " + folder.UnreadItemCount);
    Console.WriteLine("Whether this folder has subfolders: {0}", (folder.HasSubFolders)?"Yes":"No");
    Console.WriteLine("------------------Next Folder--------------------");
}
Console.WriteLine("Completed");
[VB.NET]
Dim olf As New OutlookFile("C:\Users\jack\Documents\Outlook Files\Sample.pst")
Dim folderCollection As OutlookFolderCollection = olf.RootOutlookFolder.GetSubFolders()

For Each folder As OutlookFolder In folderCollection
    Console.WriteLine("Folder: " + folder.Name)

    Console.WriteLine("Total items: " + folder.ItemCount)
	Console.WriteLine("Total unread items: " + folder.UnreadItemCount)
	Console.WriteLine("Whether this folder has subfolders: {0}", If((folder.HasSubFolders), "Yes", "No"))
	Console.WriteLine("------------------Next Folder--------------------")
Next
Console.WriteLine("Completed")