ServerDocument 构造函数

定义

重载

ServerDocument(String)

使用要加载的文档的完整路径,初始化 ServerDocument 类的新实例。

ServerDocument(Byte[], String)

使用表示要加载的文档的字节数组和该文档的文件扩展名,初始化 ServerDocument 类的新实例。

ServerDocument(Stream, String)

使用表示要加载的文档的流和该文档的文件扩展名,初始化 ServerDocument 类的新实例。

ServerDocument(String, FileAccess)

使用要加载的文档的完整路径和一个指示对该文档的文件访问权限的值,初始化 ServerDocument 类的新实例。

ServerDocument(String)

使用要加载的文档的完整路径,初始化 ServerDocument 类的新实例。

public:
 ServerDocument(System::String ^ documentPath);
public ServerDocument (string documentPath);
new Microsoft.VisualStudio.Tools.Applications.ServerDocument : string -> Microsoft.VisualStudio.Tools.Applications.ServerDocument
Public Sub New (documentPath As String)

参数

documentPath
String

要加载的文档的完整路径。

例外

documentPath 参数为 null 或为空,或者完全由空白字符组成。

documentPath 指定的文件不存在。

documentPath 指定的文件具有并非通过 Visual Studio 2010 Tools for Office Runtime 或 Visual Studio Tools for Microsoft Office System(3.0 版 Runtime)创建的自定义。

指定的documentPath文件具有Visual Studio Tools for Office运行时不支持的文件扩展名。

示例

下面的代码示例使用 ServerDocument(String) 构造函数创建加载指定文档的新 ServerDocument 。 然后,该示例显示附加到文档的自定义项的部署清单的 URL。

此示例需要:

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

  • 对以下程序集的引用:

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

    • 如果项目面向 .NET Framework 3.5) ,Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (。

  • Imports (for Visual Basic) 或using代码文件顶部的 和 Microsoft.VisualStudio.Tools.Applications.Runtime 命名空间的 C#) 语句Microsoft.VisualStudio.Tools.Applications的 (。

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

注解

使用此构造函数访问磁盘上的文档中的缓存数据或部署清单信息。 使用此构造函数时,将打开具有读/写访问权限的指定文档。

适用于

ServerDocument(Byte[], String)

使用表示要加载的文档的字节数组和该文档的文件扩展名,初始化 ServerDocument 类的新实例。

public:
 ServerDocument(cli::array <System::Byte> ^ bytes, System::String ^ fileType);
public ServerDocument (byte[] bytes, string fileType);
new Microsoft.VisualStudio.Tools.Applications.ServerDocument : byte[] * string -> Microsoft.VisualStudio.Tools.Applications.ServerDocument
Public Sub New (bytes As Byte(), fileType As String)

参数

bytes
Byte[]

一个表示要加载的文档的字节数组。

fileType
String

存储在 参数中的 bytes 文档文件扩展名,前面是句点 (.) ,例如“.xlsx”或“.docx”。

例外

参数 bytesnull 或 empty。-or-The fileType 参数为 null 或为空,或完全由空格字符组成。

参数fileType指定Visual Studio Tools for Office运行时不支持的文件扩展名。

documentPath 指定的文件具有并非通过 Visual Studio 2010 Tools for Office Runtime 或 Visual Studio Tools for Microsoft Office System(3.0 版 Runtime)创建的自定义。

示例

下面的代码示例使用 [ServerDocument 构造函数 (Byte<xref:Microsoft.VisualStudio.Tools.Applications.ServerDocument.%23ctor%28System.Byte%5B%5D%2CSystem。 String%29> 构造函数,用于从包含具有 .xlsx 文件扩展名的 Excel 工作簿的字节数组创建新的 [ServerDocument 构造函数 (Byte<xref:Microsoft.VisualStudio.Tools.Applications.ServerDocument> 。 然后,该示例使用 [ServerDocument 构造函数 (Byte<xref:Microsoft.VisualStudio.Tools.Applications.ServerDocument.Document%2A> 属性显示文档中的字节数。

此示例需要:

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

  • 对以下程序集的引用:

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

    • 如果项目面向 .NET Framework 3.5) ,Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (。

  • Imports (for Visual Basic) 或 using (代码文件顶部的 [ServerDocument 构造函数 (Byte<xref:Microsoft.VisualStudio.Tools.Applications?displayProperty=fullName> 和 [ServerDocument 构造函数 (Byte<xref:Microsoft.VisualStudio.Tools.Applications.Runtime?displayProperty=fullName 命名空间的 ) C#> (。

private void CreateServerDocumentFromByteArray(string documentPath)
{
    int runtimeVersion = 0;
    ServerDocument serverDocument1 = null;
    System.IO.FileStream stream = null;

    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
        if (runtimeVersion == 3)
        {
            // Read the file into a byte array.
            stream = new System.IO.FileStream(
                documentPath, System.IO.FileMode.Open,
                System.IO.FileAccess.Read);
            byte[] buffer = new byte[(int)stream.Length];
            stream.Read(buffer, 0, (int)buffer.Length);

            // Display the number of bytes in the document.
            serverDocument1 = new ServerDocument(buffer,
                "*.xlsx");
            MessageBox.Show("The Document property contains " +
                serverDocument1.Document.Length.ToString() +
                " bytes.");
        }
    }
    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();
        if (stream != null)
            stream.Close();
    }
}
Private Sub CreateServerDocumentFromByteArray(ByVal documentPath As String)
    Dim runtimeVersion As Integer = 0
    Dim serverDocument1 As ServerDocument = Nothing
    Dim stream As System.IO.FileStream = Nothing

    Try
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
        If runtimeVersion = 3 Then
            ' Read the file into a byte array.
            stream = New System.IO.FileStream(documentPath, System.IO.FileMode.Open, _
                System.IO.FileAccess.Read)
            Dim buffer(Fix(stream.Length)) As Byte
            stream.Read(buffer, 0, Fix(buffer.Length))

            ' Display the number of bytes in the document.
            serverDocument1 = New ServerDocument(buffer, "*.xlsx")
            MessageBox.Show("The Document property contains " & _
                serverDocument1.Document.Length.ToString() & " bytes.")
        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
        If Not (stream Is Nothing) Then
            stream.Close()
        End If
    End Try
End Sub

注解

使用此构造函数访问已在内存中的文档中的缓存数据或部署清单信息。 使用此构造函数时,文档以读/写访问权限打开。

参数 fileType 仅用于确定存储在字节数组中的文档的类型。 的值 fileType 映射到文档级自定义项支持的文件类型之一。 未尝试打开该文件。 可以选择传入完整的文件名 (例如“Workbook1.xlsx”) ,但如果这样做,则仅使用文件扩展名。 有关支持的文件类型的详细信息,请参阅 Document-Level 自定义项的体系结构

若要在调用此构造函数后访问文档的字节数组,请使用 [ServerDocument 构造函数 (Byte<xref:Microsoft.VisualStudio.Tools.Applications.ServerDocument.Document%2A> 属性。

适用于

ServerDocument(Stream, String)

使用表示要加载的文档的流和该文档的文件扩展名,初始化 ServerDocument 类的新实例。

public:
 ServerDocument(System::IO::Stream ^ stream, System::String ^ fileType);
public ServerDocument (System.IO.Stream stream, string fileType);
new Microsoft.VisualStudio.Tools.Applications.ServerDocument : System.IO.Stream * string -> Microsoft.VisualStudio.Tools.Applications.ServerDocument
Public Sub New (stream As Stream, fileType As String)

参数

stream
Stream

表示要加载的文档的流。

fileType
String

存储在 参数中的 bytes 文档文件扩展名,前面是句点 (.) ,例如“.xlsx”或“.docx”。

例外

参数 streamnull 或 empty。-or-The fileType 参数为 null 或为空,或完全由空格字符组成。

stream 参数的长度为零或其当前位置位于流的末尾。

参数fileType指定Visual Studio Tools for Office运行时不支持的文件扩展名。

documentPath 指定的文件具有并非通过 Visual Studio 2010 Tools for Office Runtime 或 Visual Studio Tools for Microsoft Office System(3.0 版 Runtime)创建的自定义。

示例

下面的代码示例使用 ServerDocument(Stream, String) 构造函数从FileStream包含文件扩展名为 .xlsx 的 Excel 工作簿的 创建一个新的 ServerDocument 。 然后,代码显示附加到文档的自定义项的部署清单的 URL。

此示例需要:

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

  • 对以下程序集的引用:

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

    • 如果项目面向 .NET Framework 3.5) ,Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (。

  • Imports (for Visual Basic) 或using代码文件顶部的 和 Microsoft.VisualStudio.Tools.Applications.Runtime 命名空间的 C#) 语句Microsoft.VisualStudio.Tools.Applications的 (。

private void CreateServerDocumentFromStream(string documentPath)
{
    int runtimeVersion = 0;
    ServerDocument serverDocument1 = null;
    System.IO.FileStream stream = null;

    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
        if (runtimeVersion == 3)
        {
            stream = new System.IO.FileStream(
                documentPath, System.IO.FileMode.Open);
            serverDocument1 = new ServerDocument(stream,
                "*.xlsx");
            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();
        if (stream != null)
            stream.Close();
    }
}
Private Sub CreateServerDocumentFromStream(ByVal documentPath As String)
    Dim runtimeVersion As Integer = 0
    Dim serverDocument1 As ServerDocument = Nothing
    Dim stream As System.IO.FileStream = Nothing

    Try
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
        If runtimeVersion = 3 Then
            stream = New System.IO.FileStream(documentPath, System.IO.FileMode.Open)
            serverDocument1 = New ServerDocument(stream, "*.xlsx")
            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
        If Not (stream Is Nothing) Then
            stream.Close()
        End If
    End Try
End Sub

注解

使用此构造函数访问已在内存中的文档中的缓存数据或部署清单信息。 使用此构造函数时,文档以读/写访问权限打开。

参数 fileType 仅用于确定存储在字节数组中的文档的类型。 的值 fileType 映射到文档级自定义项支持的文件类型之一。 未尝试打开该文件。 可以选择传入完整的文件名 (例如“Workbook1.xlsx”) ,但如果这样做,则仅使用文件扩展名。 有关支持的文件类型的详细信息,请参阅 Document-Level 自定义项的体系结构

若要在调用此构造函数后访问文档的字节数组,请使用 Document 属性。

适用于

ServerDocument(String, FileAccess)

使用要加载的文档的完整路径和一个指示对该文档的文件访问权限的值,初始化 ServerDocument 类的新实例。

public:
 ServerDocument(System::String ^ documentPath, System::IO::FileAccess access);
public ServerDocument (string documentPath, System.IO.FileAccess access);
new Microsoft.VisualStudio.Tools.Applications.ServerDocument : string * System.IO.FileAccess -> Microsoft.VisualStudio.Tools.Applications.ServerDocument
Public Sub New (documentPath As String, access As FileAccess)

参数

documentPath
String

要加载的文档的完整路径。

access
FileAccess

一个指示对文档的文件访问权限的值。

例外

documentPath 参数为 null 或为空,或者完全由空白字符组成。

access 值为 System.IO.FileAccess.Write。

documentPath 指定的文件不存在。

参数 documentPath 指定的文件没有自定义项,并且 的 access 值为 System.IO.FileAccess.Read。

documentPath 指定的文件具有并非通过 Visual Studio 2010 Tools for Office Runtime 或 Visual Studio Tools for Microsoft Office System(3.0 版 Runtime)创建的自定义。

指定的documentPath文件具有Visual Studio Tools for Office运行时不支持的文件扩展名。

示例

下面的代码示例使用 ServerDocument(String, FileAccess) 构造函数创建一个新的 ServerDocument ,用于加载具有只读访问权限的指定文档。 然后,代码显示附加到文档的自定义项的部署清单的 URL。

此示例需要:

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

  • 对以下程序集的引用:

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

    • 如果项目面向 .NET Framework 3.5) ,Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (。

  • Imports (for Visual Basic) 或using代码文件顶部的 和 Microsoft.VisualStudio.Tools.Applications.Runtime 命名空间的 C#) 语句Microsoft.VisualStudio.Tools.Applications的 (。

private void CreateServerDocumentReadOnly(string documentPath)
{
    int runtimeVersion = 0;
    ServerDocument serverDocument1 = null;

    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
        if (runtimeVersion == 3)
        {
            serverDocument1 = new ServerDocument(documentPath,
                System.IO.FileAccess.Read);
            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 (DocumentNotCustomizedException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document does not " +
            "have a customization.");
    }
    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();
    }
}
Private Sub CreateServerDocumentReadOnly(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, System.IO.FileAccess.Read)
            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 DocumentNotCustomizedException
        System.Windows.Forms.MessageBox.Show("The specified document does not " & _
            "have a customization.")
    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

注解

如果要以只读或只写访问权限打开文档,请使用此构造函数访问磁盘上的文档中的缓存数据或部署清单信息。 默认情况下,其他 ServerDocument 构造函数以读/写访问权限打开文档。

适用于