Share via


ServerDocument 建構函式 (array<Byte , String)

使用位元組陣列和文件的副檔名,初始化 ServerDocument 類別的新執行個體,位元組陣列表示要載入的文件。

命名空間:  Microsoft.VisualStudio.Tools.Applications
組件:  Microsoft.VisualStudio.Tools.Applications.ServerDocument (在 Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 中)

語法

'宣告
Public Sub New ( _
    bytes As Byte(), _
    fileType As String _
)
public ServerDocument(
    byte[] bytes,
    string fileType
)

參數

  • bytes
    型別:array<System.Byte[]
    位元組陣列,表示要載入的文件。
  • fileType
    型別:System.String
    bytes 參數中所儲存的文件的副檔名,前面加上句號 (.),例如 ".xlsx" 或 ".docx"。

例外狀況

例外狀況 條件
ArgumentNullException

bytes 參數為 nullNull 參照 (即 Visual Basic 中的 Nothing) 或空白。

-或-

fileType 參數為 nullNull 參照 (即 Visual Basic 中的 Nothing),或空字串,或整個都是空白字元。

UnknownCustomizationFileException

Visual Studio Tools for Office Runtime 不支援 fileType 參數所指定的副檔名。

DocumentCustomizedWithPreviousRuntimeException

documentPath 指定的檔案具有不以 Visual Studio 2010 Tools for Office Runtime 或 Visual Studio Tools for the Microsoft Office 系統 (3.0 版執行階段) 建立的自訂。

備註

使用這個建構函式存取記憶體中文件的快取資料或部署資訊清單資訊。 當您使用這個建構函式時,文件會以讀取/寫入存取權開啟。

fileType 參數僅用來判斷位元組陣列中儲存的文件類型。 fileType 的值會對應到文件層級自訂的其中一個支援的檔案類型。 不會嘗試開啟這個檔案。 您可以選擇傳入完整檔名 (例如,"Workbook1.xlsx"),但這樣做只會使用副檔名。 如需有關支援的檔案類型之詳細資訊,請參閱文件層級自訂的架構

若要在呼叫這個建構函式之後存取文件的位元組陣列,請使用 Document 屬性。

範例

下列程式碼範例會使用 ServerDocument(array<Byte[], String) 建構函式,從包含 Excel 活頁簿 (具有 .xlsx 副檔名) 的位元組陣列中建立新的 ServerDocument。 範例接著會使用 Document 屬性,顯示文件中位元組的數目。

這個範例需要:

  • 主控台應用程式專案或其他非 Office 專案。

  • 下列組件的參考:

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.dll (如果專案的目標是 .NET Framework 4)。

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (如果專案的目標是 .NET Framework 3.5)。

  • Microsoft.VisualStudio.Tools.ApplicationsMicrosoft.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 類別

ServerDocument 多載

Microsoft.VisualStudio.Tools.Applications 命名空間