Share via


ServerDocument.Document, propriété

Obtient le tableau d'octets d'un document en mémoire chargé dans le ServerDocument.

Espace de noms :  Microsoft.VisualStudio.Tools.Applications
Assembly :  Microsoft.VisualStudio.Tools.Applications.ServerDocument (dans Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll)

Syntaxe

'Déclaration
Public ReadOnly Property Document As Byte()
public byte[] Document { get; }

Valeur de propriété

Type : array<System.Byte[]
Tableau d'octets d'un document en mémoire chargé dans le ServerDocument.

Exceptions

Exception Condition
DocumentClosedException

Le document a été fermé.

Notes

Cette propriété retourne un tableau d'octets rempli si ServerDocument a été créé en utilisant le constructeur ServerDocument(array<Byte[], String) qui a un paramètre de tableau d'octets, ou le constructeur ServerDocument(Stream, String) qui a un paramètre Stream.Sinon, cette propriété retourne un tableau d'octets vide.

L'utilisation de cette propriété vous permet d'apporter des modifications à un document et d'envoyer ce dernier à un client sans l'écrire sur le disque.

Exemples

L'exemple de code suivant utilise le constructeur ServerDocument(array<Byte[], String) pour créer un ServerDocument à partir d'un tableau d'octets qui contient un classeur Excel avec l'extension de nom de fichier .xlsx.L'exemple utilise ensuite la propriété Document pour afficher le nombre d'octets contenus dans le document.

Cet exemple nécessite :

  • Projet d'application console ou un autre projet non-Office.

  • Références aux assemblys suivants :

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll et Microsoft.VisualStudio.Tools.Applications.Runtime.dll (si le projet cible .NET Framework 4 ou .NET Framework 4.5).

      ou

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll et Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (si le projet cible le .NET Framework 3.5).

  • Instructions Imports (pour Visual Basic) ou using pour C#) pour les espaces de noms Microsoft.VisualStudio.Tools.Applications et Microsoft.VisualStudio.Tools.Applications.Runtime au haut de votre fichier de code.

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

Sécurité .NET Framework

Voir aussi

Référence

ServerDocument Classe

Microsoft.VisualStudio.Tools.Applications, espace de noms