Home > Code > C# > How to Split Single PST File into Multiple PSTs & Merge Multiple PSTs using .NET

How to Split Single PST File into Multiple PSTs & Merge Multiple PSTs using .NET

by sher azam   on Jun 24, 2015   Category: C#   |  Views: 708    |  Points: 25   |  Gold 


This technical tip explains how to .NET developers can split and Merge PST files using Aspose.Email. Aspose.Email API provides the capability to split a single PST file into multiple PST files of desired file size. It can also merge multiple PST files into a single PST file. Both the splitting and merging of PSTs operations can be tracked by adding events to these operations.

//Code sample for Splitting a Single PST into multiple PSTs

[C#]

using (PersonalStorage pst = PersonalStorage.FromFile(@"D:\test\source.pst"))
{
// The events subscription is an optional step for the tracking process only.
pst.StorageProcessed += PstSplit_OnStorageProcessed;
pst.ItemMoved += PstSplit_OnItemMoved;

// Splits into pst chunks with the size of 5mb
pst.SplitInto(5000000, @"D:\test\chunks\");
}


[VB.NET]

Using pst As PersonalStorage = PersonalStorage.FromFile("D:\test\source.pst")
' The events subscription is an optional step for the tracking process only.
pst.StorageProcessed += PstSplit_OnStorageProcessed
pst.ItemMoved += PstSplit_OnItemMoved

' Splits into pst chunks with the size of 5mb
pst.SplitInto(5000000, "D:\test\chunks\")


//Code sample for Merging of Multiple PSTs into a single PST

[C#]

totalAdded = 0;

using (PersonalStorage pst = PersonalStorage.FromFile(@"D:\test\destination.pst"))
{
// The events subscription is an optional step for the tracking process only.
pst.StorageProcessed += PstMerge_OnStorageProcessed;
pst.ItemMoved += PstMerge_OnItemMoved;

// Merges with the pst files that are located in separate folder.
pst.MergeWith(Directory.GetFiles(@"D:\test\sources\"));
Console.WriteLine("Total messages added: {0}", totalAdded);
}

[VB.NET]

totalAdded = 0

Using pst As PersonalStorage = PersonalStorage.FromFile("D:\test\destination.pst")
' The events subscription is an optional step for the tracking process only.
pst.StorageProcessed += PstMerge_OnStorageProcessed
pst.ItemMoved += PstMerge_OnItemMoved

' Merges with the pst files that are located in separate folder.
pst.MergeWith(Directory.GetFiles("D:\test\sources\"))
Console.WriteLine("Total messages added: {0}", totalAdded)
End Using

//Code sample Merging Folders from another PST

[C#]

totalAdded = 0;

using (PersonalStorage destinationPst = PersonalStorage.FromFile(@"D:\test\destination.pst"))
using (PersonalStorage sourcePst = PersonalStorage.FromFile(@"D:\test\source.pst"))
{
FolderInfo destinationFolder = destinationPst.RootFolder.AddSubFolder("FolderFromAnotherPst");
FolderInfo sourceFolder = sourcePst.GetPredefinedFolder(StandardIpmFolder.DeletedItems);

// The events subscription is an optional step for the tracking process only.
destinationFolder.ItemMoved += destinationFolder_ItemMoved;

// Merges with the folder from another pst.
destinationFolder.MergeWith(sourceFolder);

Console.WriteLine("Total messages added: {0}", totalAdded);
}

[VB.NET]

totalAdded = 0

Using destinationPst As PersonalStorage = PersonalStorage.FromFile("D:\test\destination.pst")
Using sourcePst As PersonalStorage = PersonalStorage.FromFile("D:\test\source.pst")
Dim destinationFolder As FolderInfo = destinationPst.RootFolder.AddSubFolder("FolderFromAnotherPst")t
Dim sourceFolder As FolderInfo = sourcePst.GetPredefinedFolder(StandardIpmFolder.DeletedItems)

' The events subscription is an optional step for the tracking process only.
destinationFolder.ItemMoved += destinationFolder_ItemMoved

' Merges with the folder from another pst.
destinationFolder.MergeWith(sourceFolder)

Console.WriteLine("Total messages added: {0}", totalAdded)
End Using
End Using
//Helping Methods
void destinationFolder_ItemMoved(object sender, ItemMovedEventArgs e)
{
totalAdded++;
}

void PstMerge_OnStorageProcessed(object sender, StorageProcessedEventArgs e)
{
Console.WriteLine("*** The storage is merging: {0}", e.FileName);
}

void PstMerge_OnItemMoved(object sender, ItemMovedEventArgs e)
{
if (currentFolder == null)
{
currentFolder = e.DestinationFolder.RetrieveFullPath();
}

string folderPath = e.DestinationFolder.RetrieveFullPath();

if (currentFolder != folderPath)
{
Console.WriteLine(" Added {0} messages to \"{1}\"", messageCount, currentFolder);
messageCount = 0;
currentFolder = folderPath;
}

messageCount++;
totalAdded++;
}

void PstSplit_OnStorageProcessed(object sender, StorageProcessedEventArgs e)
{
if (currentFolder != null)
{
Console.WriteLine(" Added {0} messages to \"{1}\"", messageCount, currentFolder);
}

messageCount = 0;
currentFolder = null;
Console.WriteLine("*** The chunk is processed: {0}", e.FileName);
}

void PstSplit_OnItemMoved(object sender, ItemMovedEventArgs e)
{
if (currentFolder == null)
{
currentFolder = e.DestinationFolder.RetrieveFullPath();
}

string folderPath = e.DestinationFolder.RetrieveFullPath();

if (currentFolder != folderPath)
{
Console.WriteLine(" Added {0} messages to \"{1}\"", messageCount, currentFolder);
messageCount = 0;
currentFolder = folderPath;
}

messageCount++;
}

Overview: Aspose.Email for .NET

Aspose.Email for .NET is a set of components allowing developers to easily implement email functionality within their ASP.NET web applications, web services & Windows applications. It Supports Outlook PST, EML, MSG & MHT formats. It allows developers to work with SMTP, POP3, FTP & MS Exchange servers. It supports mail merge, iCalendar, customized header & body, header information, embedded files, Twitter & many more. It makes it easy to work with HTML or plain text emails & their attachments.

- Homepage of Aspose.Email for .NET: http://www.aspose.com/.net/email-component.aspx

- Download Aspose.Email for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.email-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts