Share via


ServerDocument.IsCacheEnabled, méthode

Obtient une valeur qui indique si le document spécifié a un cache de données.

Espace de noms :  Microsoft.VisualStudio.Tools.Applications
Assembly :  Microsoft.VisualStudio.Tools.Applications.ServerDocument (dans Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll)

Syntaxe

'Déclaration
Public Shared Function IsCacheEnabled ( _
    documentPath As String _
) As Boolean
public static bool IsCacheEnabled(
    string documentPath
)

Paramètres

  • documentPath
    Type : System.String
    Chemin d'accès complet du document à vérifier.

Valeur de retour

Type : System.Boolean
true si le document spécifié a un cache de données ; sinon, false.

Exceptions

Exception Condition
ArgumentNullException

Le paramètre documentPath est nullune référence null (Nothing en Visual Basic), est vide ou ne contient que des espaces blancs.

FileNotFoundException

Le fichier spécifié par documentPath n'existe pas.

DocumentCustomizedWithPreviousRuntimeException

Le fichier spécifié par documentPath a une personnalisation qui n'a pas été créée avec Visual Studio 2010 tools pour Office Runtime ou Visual Studio Tools pour Microsoft Office system (version 3,0 runtime).

Notes

Cette méthode indique si le document a un cache de données, mais pas si le cache de données contient réellement des données.Si le document a un cache de données qui ne contient pas de données, cette méthode retourne toujours true.

Exemples

L'exemple de code suivant crée un ServerDocument, puis utilise la propriété CachedData pour afficher le contenu du cache de données.Il utilise d'abord la méthode IsCacheEnabled pour vérifier que le classeur a un cache de données.

Cet exemple nécessite :

  • Projet d'application console ou un autre projet non-Office.

  • Références aux assemblys suivants :

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll et Microsoft.VisualStudio.Tools.Applications.Runtime.dll (si le projet cible .NET Framework 4 ou .NET Framework 4.5).

      ou

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll et Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (si le projet cible le .NET Framework 3.5).

  • Instructions Imports (pour Visual Basic) ou using pour C#) pour les espaces de noms Microsoft.VisualStudio.Tools.Applications et Microsoft.VisualStudio.Tools.Applications.Runtime au haut de votre fichier de code.

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();
    }
}

Sécurité .NET Framework

Voir aussi

Référence

ServerDocument Classe

Microsoft.VisualStudio.Tools.Applications, espace de noms