ServerDocument.Document Eigenschaft

Definition

Ruft das Bytearray des Dokuments ab, das in das ServerDocument geladen wird.

public byte[] Document { get; }

Eigenschaftswert

Byte[]

Das Bytearray eines speicherresidenten Dokuments, das in das ServerDocument geladen wird.

Ausnahmen

Das Dokument wurde geschlossen.

Beispiele

Im folgenden Codebeispiel wird der- ServerDocument(Byte[], String) Konstruktor verwendet, um eine neue ServerDocument aus einem Bytearray zu erstellen, das eine Excel-Arbeitsmappe mit der Dateinamenerweiterung ". xlsx" enthält. Im Beispiel wird dann die- Document Eigenschaft verwendet, um die Anzahl der Bytes im Dokument anzuzeigen.

Für dieses Beispiel benötigen Sie Folgendes:

  • Ein Konsolen Anwendungsprojekt oder ein anderes nicht-Office-Projekt.

  • Verweise auf die folgenden Assemblys:

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll und Microsoft.VisualStudio.Tools.Applications.Runtime.dll (wenn das Projekt .NET Framework 4 oder .NET Framework 4.5 als Ziel hat).

      oder

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll und Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (wenn das Projekt auf die .NET Framework 3,5) abzielt.

  • Imports(für Visual Basic) oder using (für c#)-Anweisungen für Microsoft.VisualStudio.Tools.Applications -und- Microsoft.VisualStudio.Tools.Applications.Runtime Namespaces am Anfang der Codedatei.

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

Hinweise

Diese Eigenschaft gibt ein ausgefülltes Bytearray zurück, wenn mit ServerDocument dem ServerDocument(Byte[], String) Konstruktor mit einem Bytearray-Parameter oder dem ServerDocument(Stream, String) Konstruktor mit einem-Parameter erstellt wurde Stream . Andernfalls gibt diese Eigenschaft ein leeres Bytearray zurück.

Wenn Sie diese Eigenschaft verwenden, können Sie Änderungen an einem Dokument vornehmen und an einen Client senden, ohne das Dokument auf den Datenträger zu schreiben.

Gilt für