ServerDocument.IsCacheEnabled 方法

取得值,指出指定的文件是否含有資料快取。

命名空間:  Microsoft.VisualStudio.Tools.Applications
組件:  Microsoft.VisualStudio.Tools.Applications.ServerDocument (在 Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 中)

語法

'宣告
Public Shared Function IsCacheEnabled ( _
    documentPath As String _
) As Boolean
public static bool IsCacheEnabled(
    string documentPath
)

參數

  • documentPath
    型別:System.String
    您想要檢查之文件的完整路徑。

傳回值

型別:System.Boolean
如果指定的文件含有資料快取則為 true,否則為 false。

例外狀況

例外狀況 條件
ArgumentNullException

documentPath 參數為 nullNull 參照 (即 Visual Basic 中的 Nothing),或空字串,或整個都是空白字元。

FileNotFoundException

documentPath 所指定的檔案不存在。

DocumentCustomizedWithPreviousRuntimeException

documentPath 指定的檔案具有不以 Visual Studio 2010 Tools for Office Runtime 或 Visual Studio Tools for the Microsoft Office 系統 (3.0 版執行階段) 建立的自訂。

備註

這個方法只表示文件是否含有資料快取,而不表示資料快取實際上是否包含資料。 如果文件具有不含任何資料的資料快取,這個方法仍然會傳回 true。

範例

下列程式碼範例會建立新的 ServerDocument,然後使用 CachedData 屬性顯示資料快取內容。 範例首先會使用 IsCacheEnabled 方法,確認活頁簿含有資料快取。

這個範例需要:

  • 主控台應用程式專案或其他非 Office 專案。

  • 下列組件的參考:

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.dll (如果專案的目標是 .NET Framework 4)。

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (如果專案的目標是 .NET Framework 3.5)。

  • Microsoft.VisualStudio.Tools.ApplicationsMicrosoft.VisualStudio.Tools.Applications.Runtime 命名空間 (在程式碼檔最頂端) 的Imports (Visual Basic) 或 using (C#) 陳述式。

Private Sub DisplayDataCacheContents(ByVal documentPath As String)
    Dim runtimeVersion As Integer = 0
    Dim serverDocument1 As ServerDocument = Nothing

    Try
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
        If runtimeVersion <> 3 Then
            MessageBox.Show("This document does not have a Visual Studio Tools for Office " & _
                "customization, or it has a customization that was created with a version of " & _
                "the runtime that is incompatible with this version of the ServerDocument class.")
            Return
        End If

        If ServerDocument.IsCacheEnabled(documentPath) Then
            serverDocument1 = New ServerDocument(documentPath)
            Dim stringBuilder1 As New System.Text.StringBuilder()

            ' Display all of the cached data items 
            ' in the document.
            Dim hostItem1 As CachedDataHostItem
            For Each hostItem1 In serverDocument1.CachedData.HostItems
                stringBuilder1.Append(vbLf & "Namespace and class: ")
                stringBuilder1.Append(hostItem1.Id & vbLf)
                Dim dataItem1 As CachedDataItem
                For Each dataItem1 In hostItem1.CachedData
                    stringBuilder1.Append("     Data item: ")
                    stringBuilder1.Append(dataItem1.Id & vbLf)
                Next dataItem1
            Next hostItem1
            MessageBox.Show(stringBuilder1.ToString())
        Else
            MessageBox.Show("The specified document does not have cached data.")
        End If

    Catch ex As System.IO.FileNotFoundException
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.")
    Catch ex As UnknownCustomizationFileException
        System.Windows.Forms.MessageBox.Show("The specified document has a file " & _
            "extension that is not supported by Visual Studio Tools for Office.")
    Finally
        If Not (serverDocument1 Is Nothing) Then
            serverDocument1.Close()
        End If
    End Try
End Sub
private void DisplayDataCacheContents(string documentPath)
{
    int runtimeVersion = 0;
    ServerDocument serverDocument1 = null;

    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);

        if (runtimeVersion != 3)
        {
            MessageBox.Show("This document does not have a Visual Studio Tools for " +
                "Office customization, or it has a customization that was created with " +
                "a version of the runtime that is incompatible with this version of the " +
                "ServerDocument class.");
            return;
        }

        if (ServerDocument.IsCacheEnabled(documentPath))
        {
            serverDocument1 = new ServerDocument(documentPath);
            System.Text.StringBuilder stringBuilder1 =
                new System.Text.StringBuilder();

            // Display all of the cached data items 
            // in the document.
            foreach (CachedDataHostItem hostItem1 in
                serverDocument1.CachedData.HostItems)
            {
                stringBuilder1.Append("\nNamespace and class: ");
                stringBuilder1.Append(hostItem1.Id + "\n");
                foreach (CachedDataItem dataItem1 in
                    hostItem1.CachedData)
                {
                    stringBuilder1.Append("     Data item: ");
                    stringBuilder1.Append(dataItem1.Id + "\n");
                }
            }
            MessageBox.Show(stringBuilder1.ToString());
        }
        else
        {
            MessageBox.Show("The specified document does not have cached data.");
        }
    }
    catch (System.IO.FileNotFoundException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
    }
    catch (UnknownCustomizationFileException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document has a file " +
            "extension that is not supported by Visual Studio Tools for Office.");
    }
    finally
    {
        if (serverDocument1 != null)
            serverDocument1.Close();
    }
}

.NET Framework 安全性

請參閱

參考

ServerDocument 類別

Microsoft.VisualStudio.Tools.Applications 命名空間