共用方式為


新增或移除存放區

此範例示範如何在指定的配置檔中新增和移除存放區。

範例

此程式代碼範例示範如何在 NameSpace 對象上分別呼叫 AddStoreEx 方法和 RemoveStore 方法,以新增和移除指定配置檔中的存放區。

在 Outlook 中,您只能以程式設計方式新增或移除 PST 存放區。 下列程式代碼範例會新增 Unicode 存放區,並將 .pst 檔案放在使用者 .pst 檔案的預設位置:Documents and Settings\<UserName>\Local Settings\Application Data\Microsoft\Outlook。 程式代碼範例會使用 Environment.SpecialFolder.LocalApplicationData 來擷取 [本機設定] 資料夾下 [應用程式數據] 資料夾的路徑。 新增存放區之後,程式代碼範例會移除存放區。 因為 RemoveStore 方法需要 Folder 物件才能移除 Store 物件,所以它會列舉 Stores 集合,以尋找剛才根據 Store 物件的 FilePath 屬性新增的 Store 物件。

RemoveStore 只會從目前的配置檔中移除存放區。 它不會從檔案系統中刪除 .pst 檔案。

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The Imports or using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following lines of code show how to do the import and assignment in Visual Basic and C#.

Imports Outlook = Microsoft.Office.Interop.Outlook
using Outlook = Microsoft.Office.Interop.Outlook;
Private Sub CreateUnicodePST()
    Dim path As String = Environment.GetFolderPath( _
        Environment.SpecialFolder.LocalApplicationData) _
        & "\Microsoft\Outlook\MyUnicodeStore.pst"
    Try
        Application.Session.AddStoreEx( _
            path, Outlook.OlStoreType.olStoreUnicode)
        Dim stores As Outlook.Stores = Application.Session.Stores
        For Each store As Outlook.Store In stores
            If store.FilePath = path Then
                Dim folder As Outlook.Folder = _
                    CType(store.GetRootFolder(), Outlook.Folder)
                ' Remove the store
                Application.Session.RemoveStore(folder)
            End If
        Next
    Catch ex As Exception
        Debug.WriteLine(ex.Message)
    End Try
End Sub
private void CreateUnicodePST()
{
    string path = Environment.GetFolderPath(
        Environment.SpecialFolder.LocalApplicationData)
        + @"\Microsoft\Outlook\MyUnicodeStore.pst";
    try
    {
        Application.Session.AddStoreEx(
            path, Outlook.OlStoreType.olStoreUnicode);
        Outlook.Stores stores = Application.Session.Stores;
        foreach (Outlook.Store store in stores)
        {
            if (store.FilePath == path)
            {
               Outlook.Folder folder =
                    store.GetRootFolder() as Outlook.Folder;
               // Remove the store
               Application.Session.RemoveStore(folder);
            }
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message);
    }
}

另請參閱