次の方法で共有


ServerDocument.Document プロパティ

ServerDocument に読み込まれているメモリ内のドキュメントのバイト配列を取得します。

名前空間:  Microsoft.VisualStudio.Tools.Applications
アセンブリ:  Microsoft.VisualStudio.Tools.Applications.ServerDocument (Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 内)

構文

'宣言
Public ReadOnly Property Document As Byte()
public byte[] Document { get; }

プロパティ値

型 : array<System.Byte[]
ServerDocument に読み込まれているメモリ内のドキュメントのバイト配列です。

例外

例外 条件
DocumentClosedException

ドキュメントは閉じられました。

解説

バイト配列パラメーターを持つ ServerDocument(array<Byte[], String) コンストラクター、または Stream パラメーターを持つ ServerDocument(Stream, String) コンストラクターを使用して、ServerDocument を作成した場合、このプロパティは値の入ったバイト配列を返します。 それ以外の場合、このプロパティは空のバイト配列を返します。

このプロパティを使用すると、ディスクに書き込むことなくドキュメントを変更し、クライアントに送信できます。

次のコード例は、ServerDocument(array<Byte[], String) コンストラクターを使用して、.xlsx ファイル名拡張子が設定された Excel ブックを含むバイト配列から新しい ServerDocument を作成します。 また、Document プロパティを使用してドキュメントのバイト数を表示します。

この例には、次の項目が必要です。

  • コンソール アプリケーション プロジェクトまたはその他の 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.Applications 名前空間および Microsoft.VisualStudio.Tools.Applications.Runtime 名前空間に対する Imports ステートメント (Visual Basic の場合) または using ステートメント (C# の場合) を追加します。

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

.NET Framework セキュリティ

  • 直前の呼び出し元に対する完全な信頼。このメンバーは、部分的に信頼されているコードから使用することはできません。詳細については、「部分信頼コードからのライブラリの使用」を参照してください。

参照

関連項目

ServerDocument クラス

Microsoft.VisualStudio.Tools.Applications 名前空間