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 system (3.0 版的运行时)创建的自定义。

备注

此方法仅指示文档是否具有数据缓存,而不指示数据缓存实际是否包含数据。如果文档具有数据缓存,但其中不包含数据,此方法仍返回 true。

示例

下面的代码示例创建一个新的 ServerDocument,然后使用 CachedData 属性显示数据缓存的内容。该示例首先使用 IsCacheEnabled 方法验证工作簿是否有数据缓存。

此示例需要:

  • 控制台应用程序项目或某些其他非 Office 项目。

  • 对下列程序集的引用:

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.dll (如果项目面向 .NET Framework 4 或 .NET Framework 4.5)。

    • 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 命名空间