Class PersonalStorage

PersonalStorage class

Provides functionality to access and manipulate the PST (Personal Storage Table) files used by Microsoft Outlook.

public class PersonalStorage : IDisposable

Constructors

NameDescription
PersonalStorage(TraversalExceptionsCallback)Initializes a new instance of the PersonalStorage class that reports problems found while reading a damaged PST to a callback instead of throwing. Call Load to open the file.

Properties

NameDescription
CanWrite { get; }Gets a value indicating whether the PST is open for writing.
Format { get; }Gets the file format.
IsUnicode { get; }Gets a value indicating whether the PST is in the Unicode format. Only Unicode PST files can be edited; the older ANSI format is read-only.
RootFolder { get; }Gets the root folder of the PST, which contains all other folders.
Store { get; }Gets the PST message store.

Methods

NameDescription
static Create(Stream, FileFormatVersion)Creates a new PST in a stream.
static Create(string, FileFormatVersion)Creates a new PST file.
static Create(Stream, FileFormatVersion, bool)Creates a new PST in a stream.
static FromFile(string)Opens a PST or OST file for reading and writing.
static FromFile(string, bool)Opens a PST or OST file.
static FromFile(string, PersonalStorageLoadOptions)Opens a PST or OST file with the specified options.
static FromStream(Stream)Opens a PST or OST from a stream for reading and writing.
static FromStream(Stream, bool)Opens a PST or OST from a stream.
static FromStream(Stream, PersonalStorageLoadOptions)Opens a PST or OST from a stream with the specified options.
AddAttachmentToMessage(MessageInfo, string)Adds an attachment to the specified message using the file located at the provided path.
AddAttachmentToMessage(string, string)Adds an attachment to the message identified by the specified entry ID using the file located at the provided path.
AddAttachmentToMessage(MessageInfo, string, Stream)Adds an attachment to the specified message using the provided stream as attachment content.
AddAttachmentToMessage(string, string, Stream)Adds an attachment to the message identified by the specified entry ID using the provided stream.
ChangeMessage(string, MapiPropertyCollection)Changes properties of a message.
ConvertTo(FileFormat)Converts the open file to the specified format.
CreatePredefinedFolder(string, StandardIpmFolder)Creates a standard Outlook folder, such as Inbox or Calendar, with the specified name.
CreatePredefinedFolder(string, StandardIpmFolder, bool)Creates a standard Outlook folder, such as Inbox or Calendar, with the specified name.
DeleteItem(string)Deletes a message or folder by its entry ID.
Dispose()Closes the PST and releases the resources it uses. The underlying stream is closed too, unless the storage was opened or created with the option to leave it open.
EnumerateMessages(string)Enumerates the messages of the folder with the specified entry ID.
EnumerateMessages(string, int, int)Enumerates a range of the messages of the folder with the specified entry ID.
ExtractAttachments(MessageInfo)Reads the attachments of a message without loading the whole message.
ExtractAttachments(string)Reads the attachments of a message without loading the whole message.
ExtractMessage(byte[])Loads the full message with the specified entry ID.
ExtractMessage(MessageInfo)Loads the full message that a MessageInfo describes.
ExtractMessage(string)Loads the full message with the specified entry ID.
ExtractProperty(byte[], long)Reads one property of an item without loading the whole item.
ExtractRecipients(MessageInfo)Reads the recipients of a message without loading the whole message.
ExtractRecipients(string)Reads the recipients of a message without loading the whole message.
FindAndEnumerateSoftDeletedItems()Finds soft-deleted messages and returns them one at a time.
FindAndExtractSoftDeletedItems()Finds and extracts all soft-deleted messages of the PST.
FindMessages(string)Finds the entry IDs of the messages of a folder. Use it to read a damaged PST when FolderInfo.GetContents and FolderInfo.EnumerateMessages throw.
FindSubfolders(string)Finds the entry IDs of the subfolders of a folder. Use it to read a damaged PST when FolderInfo.GetSubFolders and FolderInfo.EnumerateFolders throw.
GetCategories()Gets the categories used in the PST, each with its name and color.
GetFolderById(byte[])Gets the folder with the specified entry ID.
GetFolderById(string)Gets the folder with the specified entry ID.
GetParentFolder(byte[])Gets the folder that contains a message or folder.
GetParentFolder(string)Gets the folder that contains a message or folder.
GetPredefinedFolder(StandardIpmFolder)Gets a standard Outlook folder of the PST, such as Inbox, Sent Items or Deleted Items.
Load(Stream)Opens a PST from a stream for reading, for a storage created with the constructor that takes a TraversalExceptionsCallback.
Load(string)Opens a PST file for reading, for a storage created with the constructor that takes a TraversalExceptionsCallback.
MergeWith(Stream[])Merges one or more other PST streams into this PST.
MergeWith(string[])Merges one or more other PST files into this PST.
MergeWithAsync(Stream[], CancellationToken)Asynchronously merges one or more other PST streams into this PST.
MergeWithAsync(string[], CancellationToken)Asynchronously merges one or more other PST files into this PST.
MoveItem(FolderInfo, FolderInfo)Moves a folder to another parent folder of the same PST.
MoveItem(MessageInfo, FolderInfo)Moves a message to another folder of the same PST.
SaveAs(Stream, FileFormat)Saves the storage to a stream in the specified format.
SaveAs(string, FileFormat)Saves the storage to another file in the specified format.
SaveMessageToStream(string, Stream)Saves a message to a stream in Outlook MSG format, without loading it into a MapiMessage.
SplitInto(IList<MailQuery>, string)Splits the PST into several files by search criteria.
SplitInto(long, string)Splits the PST into files of about the specified size.
SplitInto(IList<MailQuery>, string, string)Splits the PST into several files by search criteria, named with a prefix.
SplitInto(long, string, string)Splits the PST into files of about the specified size, named with a prefix.
SplitIntoAsync(IList<MailQuery>, string, CancellationToken)Asynchronously splits the PST into several files by search criteria.
SplitIntoAsync(long, string, CancellationToken)Asynchronously splits the PST into files of about the specified size.
SplitIntoAsync(IList<MailQuery>, string, string, CancellationToken)Asynchronously splits the PST into several files by search criteria, named with a prefix.
SplitIntoAsync(long, string, string, CancellationToken)Asynchronously splits the PST into files of about the specified size, named with a prefix.
TryToGetFolderById(string, out FolderInfo)Tries to get the folder with the specified entry ID.
TryToSaveMessage(string, Stream)Saves a message to a stream in Outlook MSG format, keeping as much of a damaged message as can be read instead of failing.
static CreateAsync(Stream, FileFormatVersion, CancellationToken)Asynchronously creates a new PST in a stream.
static CreateAsync(string, FileFormatVersion, CancellationToken)Asynchronously creates a new PST file.
static CreateAsync(Stream, FileFormatVersion, bool, CancellationToken)Asynchronously creates a new PST in a stream.
static FromFileAsync(string, CancellationToken)Asynchronously opens a PST or OST file for reading and writing.
static FromFileAsync(string, bool, CancellationToken)Asynchronously opens a PST or OST file.
static FromFileAsync(string, PersonalStorageLoadOptions, CancellationToken)Asynchronously opens a PST or OST file with the specified options.
static FromStreamAsync(Stream, CancellationToken)Asynchronously opens a PST or OST from a stream for reading and writing.
static FromStreamAsync(Stream, bool, CancellationToken)Asynchronously opens a PST or OST from a stream.
static FromStreamAsync(Stream, PersonalStorageLoadOptions, CancellationToken)Asynchronously opens a PST or OST from a stream with the specified options.

Events

NameDescription
event ItemMovedOccurs when an item is moved to another folder.
event StorageProcessedOccurs while splitting, after a new PST file has been created, and while merging, after the next source has been processed.
event StorageProcessingOccurs before the next PST file is processed while merging or splitting.

Remarks

The PersonalStorage class encapsulates methods for creating, opening, and working with the contents of PST files including emails, appointments, contacts, tasks, and other personal information.

Examples

This code serves as a way to browse and extract messages from a PST file, printing metadata about each folder and message, extract and saving the actual messages as .msg files.

[C#]

// Open the PST file by creating an instance of PersonalStorage
using (var pst = PersonalStorage.FromFile("storage.pst"))
{
    // Retrieve the total number of items in the PST file
    var totalItemsCount = pst.Store.GetTotalItemsCount();
    
    // Write the total items count to the console
    Console.WriteLine($"Total items count: {totalItemsCount}");

    // Iterate through each subfolder within the root folder of the PST
    foreach (var folderInfo in pst.RootFolder.GetSubFolders())
    {
        // Write the display name of the folder to the console
        Console.WriteLine($"Folder: {folderInfo.DisplayName}");
        // Write the total number of items in the folder to the console
        Console.WriteLine($"Total items: {folderInfo.ContentCount}");
        // Write the count of unread items in the folder to the console
        Console.WriteLine($"Total unread items: {folderInfo.ContentUnreadCount}");

        // Enumerate through each message in the current folder
        foreach (var messageInfo in folderInfo.EnumerateMessages())
        {
            // Write the subject of the message to the console
            Console.WriteLine($"Subject: {messageInfo.Subject}");
            
            // Extract the full message object from the messageInfo
            var msg = pst.ExtractMessage(messageInfo);
            
            // Save the message as a .msg file, using its subject as the filename
            msg.Save($"{msg.Subject}.msg");
        }
    }
}

[Visual Basic]

' Open the PST file by creating an instance of PersonalStorage
Using pst As PersonalStorage = PersonalStorage.FromFile("storage.pst")
    ' Retrieve the total number of items in the PST file
    Dim totalItemsCount As Integer = pst.Store.GetTotalItemsCount()
    
    ' Write the total items count to the console
    Console.WriteLine($"Total items count: {totalItemsCount}")

    ' Iterate through each subfolder within the root folder of the PST
    For Each folderInfo In pst.RootFolder.GetSubFolders()
        ' Write the display name of the folder to the console
        Console.WriteLine($"Folder: {folderInfo.DisplayName}")
        ' Write the total number of items in the folder to the console
        Console.WriteLine($"Total items: {folderInfo.ContentCount}")
        ' Write the count of unread items in the folder to the console
        Console.WriteLine($"Total unread items: {folderInfo.ContentUnreadCount}")

        ' Enumerate through each message in the current folder
        For Each messageInfo In folderInfo.EnumerateMessages()
            ' Write the subject of the message to the console
            Console.WriteLine($"Subject: {messageInfo.Subject}")
            
            ' Extract the full message object from the messageInfo
            Dim msg As MailItem = pst.ExtractMessage(messageInfo)
            
            ' Save the message as a .msg file, using its subject as the filename
            msg.Save($"{msg.Subject}.msg")
        Next
    Next
End Using

See Also