Export (0) Print
Expand All

ServerDocument Constructor (Byte[], String)

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Initializes a new instance of the ServerDocument class using a byte array that represents the document to be loaded and the file name extension of the document.

Namespace:  Microsoft.VisualStudio.Tools.Applications
Assembly:  Microsoft.VisualStudio.Tools.Applications.ServerDocument (in Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll)

public ServerDocument(
	byte[] bytes,
	string fileType
)

Parameters

bytes
Type: System.Byte[]

A byte array that represents the document to be loaded.

fileType
Type: System.String

The file name extension of the document that is stored in the bytes parameter, preceded by a period (.)—for example, ".xlsx" or ".docx".

ExceptionCondition
ArgumentNullException

The bytes parameter is null or empty.

-or-

The fileType parameter is null or empty or consists entirely of white space characters.

UnknownCustomizationFileException

The fileType parameter specifies a file name extension that is not supported by the Visual Studio Tools for Office runtime.

DocumentCustomizedWithPreviousRuntimeException

The file specified by documentPath has a customization that was not created with the Visual Studio 2010 Tools for Office Runtime or the Visual Studio Tools for the Microsoft Office system (version 3.0 Runtime).

Use this constructor to access the cached data or deployment manifest information in a document that is already in memory. When you use this constructor, the document is opened with read/write access.

The fileType parameter is used only to determine the type of document that is stored in the byte array. The value of fileType is mapped to one of the file types that are supported for document-level customizations. No attempt is made to open the file. You can optionally pass in a complete file name (for example, "Workbook1.xlsx"), but if you do this, only the file name extension is used. For more information about the supported file types, see Architecture of Document-Level Customizations.

To access the byte array for the document after calling this constructor, use the Document property.

The following code example uses the ServerDocument(Byte[], String) constructor to create a new ServerDocument from a byte array that contains an Excel workbook with the .xlsx file name extension. The example then uses the Document property to display the number of bytes in the document.

This example requires:

  • A console application project or some other non-Office project.

  • References to the following assemblies:

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll and Microsoft.VisualStudio.Tools.Applications.Runtime.dll (if the project targets the .NET Framework 4 or the .NET Framework 4.5).

      or

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll and Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (if the project targets the .NET Framework 3.5).

  • Imports (for Visual Basic) or using (for C#) statements for Microsoft.VisualStudio.Tools.Applications and Microsoft.VisualStudio.Tools.Applications.Runtime namespaces at the top of your code file.

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

Show:
© 2014 Microsoft