ServerDocument 类

提供对文档或工作簿(属于使用 Visual Studio 中的 Office 开发工具创建的文档级自定义项)中的缓存数据和自定义项信息的访问。

继承层次结构

System.Object
  Microsoft.VisualStudio.Tools.Applications.ServerDocument

命名空间:  Microsoft.VisualStudio.Tools.Applications
程序集:  Microsoft.VisualStudio.Tools.Applications.ServerDocument(在 Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 中)

语法

声明
<PermissionSetAttribute(SecurityAction.Demand, Name := "FullTrust")> _
Public NotInheritable Class ServerDocument _
    Implements IDisposable
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
public sealed class ServerDocument : IDisposable

ServerDocument 类型公开以下成员。

构造函数

  名称 说明
公共方法 ServerDocument(String) 使用要加载的文档的完整路径,初始化 ServerDocument 类的新实例。
公共方法 ServerDocument(array<Byte[], String) 使用表示要加载的文档的字节数组和该文档的文件扩展名,初始化 ServerDocument 类的新实例。
公共方法 ServerDocument(Stream, String) 使用表示要加载的文档的流和该文档的文件扩展名,初始化 ServerDocument 类的新实例。
公共方法 ServerDocument(String, FileAccess) 使用要加载的文档的完整路径和一个指示对该文档的文件访问权限的值,初始化 ServerDocument 类的新实例。

页首

属性

  名称 说明
公共属性 CachedData 获取一个表示文档中包含的缓存数据的 CachedData 对象。
公共属性 DeploymentManifestUrl 获取或设置与文档关联的自定义项的部署清单 URL。
公共属性 Document 获取加载到 ServerDocument 中的内存中文档的字节数组。
公共属性 SolutionId 获取 Visual Studio Tools for Office Runtime 用来标识解决方案的 GUID。

页首

方法

  名称 说明
公共方法静态成员 AddCustomization(String, Uri) 使用指定的程序集名称和部署清单,将自定义项附加到指定文档。
公共方法静态成员 AddCustomization(String, String, Guid, Uri) 使用指定的文档、程序集名称、解决方案 ID 和部署清单,将自定义项附加到指定文档。
公共方法静态成员 AddCustomization(String, String, Guid, Uri, Boolean, array<String[]%) 基础结构。
公共方法 Close 关闭 ServerDocument 对象。
公共方法 Equals 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)
受保护的方法 Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
公共方法静态成员 GetCustomizationVersion 返回用于创建与指定文档相关联的自定义项的 Visual Studio Tools for Office Runtime 的版本。
公共方法 GetHashCode 用作特定类型的哈希函数。 (继承自 Object。)
公共方法 GetType 获取当前实例的 Type。 (继承自 Object。)
公共方法静态成员 IsCacheEnabled 获取一个值,该值指示指定文档是否具有数据缓存。
公共方法静态成员 IsCustomized 获取一个值,该值指示指定文档是否具有使用 Visual Studio 2010 Tools for Office Runtime 创建的自定义项。
受保护的方法 MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
公共方法静态成员 RemoveCustomization 从文档中移除自定义项。
公共方法 Save 保存使用 ServerDocument 类对文档所做的任何更改。
公共方法 ToString 返回表示当前对象的字符串。 (继承自 Object。)

页首

显式接口实现

  名称 说明
显式接口实现私有方法 IDisposable.Dispose 释放 ServerDocument 使用的所有资源。

页首

备注

使用 ServerDocument 类可以在没有安装 Excel 或 Word 的计算机上对文档级自定义项的某些方面进行管理。 您通常在未与 Office 集成的应用程序(如控制台项目或 Windows 窗体项目)中使用此类,而不会在 Office 项目中使用。

使用 ServerDocument 类的下列成员可以执行一些常规任务:

  • 若要访问和修改服务器上文档的数据缓存中的数据,请使用 CachedData 属性。

  • 若要以编程方式将自定义项附加到文档,或从文档中移除自定义项,请使用 AddCustomizationRemoveCustomization 方法。

  • 若要访问或更改与文档关联的部署清单的 URL,请使用 DeploymentManifestUrl 属性。

Visual Studio 2010 Tools for Office Runtime中的 ServerDocument 类有两个不同的版本。 您应使用的版本取决于要在其中使用该类的应用程序的目标 .NET Framework:

  • 对于面向 .NET Framework 4 的应用程序,使用 Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 程序集中的 Microsoft.VisualStudio.Tools.Applications.ServerDocument 类。

  • 对于面向 .NET Framework 3.5 的应用程序,使用 Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll 程序集中的 Microsoft.VisualStudio.Tools.Applications.ServerDocument 类。

有关更多信息,请参见 使用 ServerDocument 类管理服务器上的文档

选择要使用的构造函数

若要使用 ServerDocument 类访问文档中的缓存数据或部署清单 URL,必须创建 ServerDocument 对象。

有两组 ServerDocument 构造函数:

  • 一组可以用来访问已在内存中打开的文档。

  • 一组可以用来访问磁盘上的文档。

访问内存中的文档

若要访问已在内存中打开的文档,请使用下面的构造函数之一:

这些构造函数接受一个字节数组或一个表示内存中的文档的 Stream。 如果想在使用 HTTP 协议将文档中的缓存数据或应用程序清单传送到目标之前对它们进行修改,这将非常有用。 若要使用这些构造函数,文档必须已经具有自定义项;否则这些构造函数将引发 CannotLoadManifestException 异常。

访问磁盘上的文档

若要访问磁盘上的文档,请使用下面的构造函数之一:

这些构造函数接受要打开的文档的完整路径。 默认情况下,文档以读/写访问权限打开。 如果想以只读或只写访问权限打开文档,请使用带有 FileAccess 参数的构造函数。

示例

下面的代码示例创建一个新的 ServerDocument,它加载指定的文档,然后显示附加到文档的自定义项的部署清单的 URL。

此示例需要:

  • 控制台应用程序项目或某些其他非 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 CreateServerDocumentFromPath(ByVal documentPath As String)
    Dim runtimeVersion As Integer = 0
    Dim serverDocument1 As ServerDocument = Nothing

    Try
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
        If runtimeVersion = 3 Then
            serverDocument1 = New ServerDocument(documentPath)
            MessageBox.Show("The URL of the deployment manifest is: " & vbLf & _
                serverDocument1.DeploymentManifestUrl.ToString())
        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 CreateServerDocumentFromPath(string documentPath)
{
    int runtimeVersion = 0;
    ServerDocument serverDocument1 = null;

    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
        if (runtimeVersion == 3)
        {
            serverDocument1 = new ServerDocument(documentPath);
            MessageBox.Show("The URL of the deployment manifest is: \n" +
                serverDocument1.DeploymentManifestUrl.ToString());
        }
    }
    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();
    }
}

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

请参见

参考

Microsoft.VisualStudio.Tools.Applications 命名空间

其他资源

使用 ServerDocument 类管理服务器上的文档

Office 解决方案中的应用程序和部署清单

访问服务器上的文档数据

如何:向服务器上的工作簿中插入数据

如何:从服务器上的工作簿中检索缓存的数据

如何:更改服务器上的工作簿中的缓存数据