Share via


ServerDocument 建構函式 (Stream, String)

使用資料流和文件副檔名,初始化 ServerDocument 類別的新執行個體,資料流表示要載入的文件。

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

語法

'宣告
Public Sub New ( _
    stream As Stream, _
    fileType As String _
)
public ServerDocument(
    Stream stream,
    string fileType
)

參數

  • fileType
    型別:System.String
    bytes 參數中所儲存的文件的副檔名,前面加上句號 (.),例如 ".xlsx" 或 ".docx"。

例外狀況

例外狀況 條件
ArgumentNullException

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

-或-

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

ArgumentException

stream 參數長度為零,或目前位置位於資料流的結尾。

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(Stream, String) 建構函式,從包含 Excel 活頁簿 (具有 .xlsx 副檔名) 的 FileStream 中建立新的 ServerDocument。 程式碼接著會顯示附加至文件之自訂的部署資訊清單 URL。

這個範例需要:

  • 主控台應用程式專案或其他非 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 CreateServerDocumentFromStream(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
            stream = New System.IO.FileStream(documentPath, System.IO.FileMode.Open)
            serverDocument1 = New ServerDocument(stream, "*.xlsx")
            MessageBox.Show("The URL of the deployment manifest is: " & vbLf & _
                serverDocument1.DeploymentManifestUrl.ToString())
        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 CreateServerDocumentFromStream(string documentPath)
{
    int runtimeVersion = 0;
    ServerDocument serverDocument1 = null;
    System.IO.FileStream stream = null;

    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
        if (runtimeVersion == 3)
        {
            stream = new System.IO.FileStream(
                documentPath, System.IO.FileMode.Open);
            serverDocument1 = new ServerDocument(stream,
                "*.xlsx");
            MessageBox.Show("The URL of the deployment manifest is: \n" +
                serverDocument1.DeploymentManifestUrl.ToString());
        }
    }
    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 命名空間