ServerDocument 类 (2007 system)

更新:2007 年 11 月

提供对文档(此文档属于 Microsoft Office Excel 2007 或 Microsoft Office Word 2007 的文档级自定义项)中的缓存数据和自定义项信息的访问。

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

语法

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

备注

使用 ServerDocument 类可以在没有安装 Excel 或 Word 的计算机上对文档级自定义项的某些方面进行管理。

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

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

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

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

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

选择要使用的构造函数

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

有两组 ServerDocument 构造函数:

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

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

访问内存中的文档

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

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

访问磁盘上的文档

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

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

了解不同版本的 ServerDocument 类

Visual Studio Tools for Office 为 Microsoft Office 2003 和 2007 Microsoft Office system 的解决方案提供了不同版本的 ServerDocument 类。此版本的 ServerDocument 类只能用于使用 Word 2007 和 Excel 2007 的文档级项目模板创建的解决方案。

若要用于使用 Word 2003 和 Excel 2003 的项目模板创建的解决方案,请使用 Microsoft.VisualStudio.Tools.Applications.Runtime 命名空间中的 ServerDocument 类。

有关更多信息,请参见使用 ServerDocument 类管理服务器上的文档。有关演示如何在同一个代码文件中使用两种版本的 ServerDocument 类的代码示例,请参见如何:编写使用两个版本的 ServerDocument 类的代码

示例

下面的代码示例创建一个新的 ServerDocument,该文档加载指定的文档,然后显示附加到文档的 Visual Studio Tools for Office 自定义项的部署清单的 URL。在创建对象之前,此代码使用 GetCustomizationVersion 方法验证该自定义项是否使用 Microsoft Visual Studio Tools for the Microsoft Office system(3.0 版运行时) 创建。这是此版本的 ServerDocument 类唯一兼容的运行时。有关更多信息,请参见使用 ServerDocument 类管理服务器上的文档

此示例需要在代码文件顶部使用对 Microsoft.VisualStudio.Tools.Applications.ServerDocument.v9.0.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll 程序集的引用,并对 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();
    }
}

继承层次结构

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

线程安全

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

另请参见

参考

ServerDocument 成员

Microsoft.VisualStudio.Tools.Applications 命名空间

其他资源

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

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

访问服务器上的文档数据

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

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

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