ServerDocument 建構函式

定義

多載

ServerDocument(String)

使用要載入之文件的完整路徑,初始化 ServerDocument 類別的新執行個體。

ServerDocument(Byte[], String)

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

ServerDocument(Stream, String)

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

ServerDocument(String, FileAccess)

使用要載入之文件的完整路徑以及表示文件檔案存取的值,初始化 ServerDocument 類別的新執行個體。

ServerDocument(String)

使用要載入之文件的完整路徑,初始化 ServerDocument 類別的新執行個體。

public:
 ServerDocument(System::String ^ documentPath);
public ServerDocument (string documentPath);
new Microsoft.VisualStudio.Tools.Applications.ServerDocument : string -> Microsoft.VisualStudio.Tools.Applications.ServerDocument
Public Sub New (documentPath As String)

參數

documentPath
String

要載入之文件的完整路徑。

例外狀況

documentPath 參數為 null,或空字串,或整個都是空白字元。

documentPath 指定的檔案不存在。

documentPath 所指定的檔案,其自訂不是使用 Visual Studio 2010 Tools for Office Runtime 或 Visual Studio Tools for the Microsoft Office system (3.0 版執行階段) 建立。

documentPath指定的檔案具有 Visual Studio Tools for Office 運行時間不支援的擴展名。

範例

下列程式代碼範例會使用 建 ServerDocument(String) 構函式來建立新的 ServerDocument ,以載入指定的檔。 然後,此範例會顯示附加至檔之自定義的部署指令清單 URL。

這個範例需要:

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

  • 下列元件的參考:

    • 如果專案以 .NET Framework 4 或 .NET Framework 4.5) 為目標,則 Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.dll (。

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

  • Imports (visual Basic) 或 using C#) 語句 Microsoft.VisualStudio.Tools.Applications 的 (和 Microsoft.VisualStudio.Tools.Applications.Runtime 程式代碼檔案頂端的 命名空間。

private void CreateServerDocumentFromPath(string documentPath)
{
    int runtimeVersion = 0;
    ServerDocument serverDocument1 = null;

    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
        if (runtimeVersion == 3)
        {
            serverDocument1 = new ServerDocument(documentPath);
            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();
    }
}
Private Sub CreateServerDocumentFromPath(ByVal documentPath As String)
    Dim runtimeVersion As Integer = 0
    Dim serverDocument1 As ServerDocument = Nothing

    Try
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
        If runtimeVersion = 3 Then
            serverDocument1 = New ServerDocument(documentPath)
            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
    End Try
End Sub

備註

使用此建構函式來存取磁碟上檔內的快取數據或部署指令清單資訊。 當您使用此建構函式時,會以讀取/寫入存取權開啟指定的檔。

適用於

ServerDocument(Byte[], String)

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

public:
 ServerDocument(cli::array <System::Byte> ^ bytes, System::String ^ fileType);
public ServerDocument (byte[] bytes, string fileType);
new Microsoft.VisualStudio.Tools.Applications.ServerDocument : byte[] * string -> Microsoft.VisualStudio.Tools.Applications.ServerDocument
Public Sub New (bytes As Byte(), fileType As String)

參數

bytes
Byte[]

位元組陣列,表示要載入的文件。

fileType
String

儲存在 參數中的 bytes 檔擴展名,前面加上句號 (.) ,例如 “.xlsx” 或 “.docx”。

例外狀況

參數 bytesnull 或 empty。-或-參數 fileTypenull 或空白,或完全包含空格符。

參數fileType會指定 Visual Studio Tools for Office 運行時間不支援的擴展名。

documentPath 所指定的檔案,其自訂不是使用 Visual Studio 2010 Tools for Office Runtime 或 Visual Studio Tools for the Microsoft Office system (3.0 版執行階段) 建立。

範例

下列程式代碼範例使用 [ServerDocument 建構函式 (Byte<xref:Microsoft.VisualStudio.Tools.Applications.ServerDocument.%23ctor%28System.Byte%5B%5D%2CSystem.. 字串%29> 建構函式,可從包含擴展名為 .xlsx 之 Excel 活頁簿的位元組陣列建立新的 [ServerDocument 建構函式 (Byte<xref:Microsoft.VisualStudio.Tools.Applications.ServerDocument> 。 然後,此範例會使用 [ServerDocument 建構函式 (Byte<xref:Microsoft.VisualStudio.Tools.Applications.ServerDocument.Document%2A> 屬性來顯示檔中的位元組數目。

這個範例需要:

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

  • 下列元件的參考:

    • 如果專案以 .NET Framework 4 或 .NET Framework 4.5) 為目標,則 Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.dll (。

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

  • Importsusing () 適用於 [ServerDocument 建構函式 (Byte<xref:Microsoft.VisualStudio.Tools.Applications?displayProperty=fullName> 和 [ServerDocument 建構函式 (Byte<xref:Microsoft.VisualStudio.Tools.Applications.Runtime?displayProperty=fullName> 命名空間的 C#) ( (。

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

備註

使用此建構函式來存取已在記憶體中的檔中快取的數據或部署指令清單資訊。 當您使用此建構函式時,會以讀取/寫入存取權開啟檔。

參數 fileType 僅用於判斷儲存在位元組數位中的檔類型。 的值 fileType 會對應至檔層級自定義支援的其中一種檔類型。 不會嘗試開啟檔案。 您可以選擇性地傳入完整的檔名 (例如“Workbook1.xlsx”) ,但如果您這麼做,則只會使用擴展名。 如需支援檔類型的詳細資訊,請參閱 Document-Level 自定義架構

若要在呼叫此建構函式之後存取檔的位元組陣列,請使用 [ServerDocument 建構函式 (Byte<xref:Microsoft.VisualStudio.Tools.Applications.ServerDocument.Document%2A> 屬性。

適用於

ServerDocument(Stream, String)

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

public:
 ServerDocument(System::IO::Stream ^ stream, System::String ^ fileType);
public ServerDocument (System.IO.Stream stream, string fileType);
new Microsoft.VisualStudio.Tools.Applications.ServerDocument : System.IO.Stream * string -> Microsoft.VisualStudio.Tools.Applications.ServerDocument
Public Sub New (stream As Stream, fileType As String)

參數

stream
Stream

資料流,表示要載入的文件。

fileType
String

儲存在 參數中的 bytes 檔擴展名,前面加上句號 (.) ,例如 “.xlsx” 或 “.docx”。

例外狀況

參數 streamnull 或 empty。-或-參數 fileTypenull 或空白,或完全包含空格符。

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

參數fileType會指定 Visual Studio Tools for Office 運行時間不支援的擴展名。

documentPath 所指定的檔案,其自訂不是使用 Visual Studio 2010 Tools for Office Runtime 或 Visual Studio Tools for the Microsoft Office system (3.0 版執行階段) 建立。

範例

下列程式代碼範例會使用 建ServerDocument(Stream, String)構函式,從 FileStream 包含擴展名為 .xlsx 的 Excel 活頁簿建立新的 ServerDocument 。 然後,程式代碼會顯示附加至檔之自定義的部署指令清單 URL。

這個範例需要:

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

  • 下列元件的參考:

    • 如果專案以 .NET Framework 4 或 .NET Framework 4.5) 為目標,則 Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.dll (。

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

  • Imports (visual Basic) 或 using C#) 語句 Microsoft.VisualStudio.Tools.Applications 的 (和 Microsoft.VisualStudio.Tools.Applications.Runtime 程式代碼檔案頂端的 命名空間。

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

備註

使用此建構函式來存取已在記憶體中的檔中快取的數據或部署指令清單資訊。 當您使用此建構函式時,會以讀取/寫入存取權開啟檔。

參數 fileType 僅用於判斷儲存在位元組數位中的檔類型。 的值 fileType 會對應至檔層級自定義支援的其中一種檔類型。 不會嘗試開啟檔案。 您可以選擇性地傳入完整的檔名 (例如“Workbook1.xlsx”) ,但如果您這麼做,則只會使用擴展名。 如需支援檔類型的詳細資訊,請參閱 Document-Level 自定義架構

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

適用於

ServerDocument(String, FileAccess)

使用要載入之文件的完整路徑以及表示文件檔案存取的值,初始化 ServerDocument 類別的新執行個體。

public:
 ServerDocument(System::String ^ documentPath, System::IO::FileAccess access);
public ServerDocument (string documentPath, System.IO.FileAccess access);
new Microsoft.VisualStudio.Tools.Applications.ServerDocument : string * System.IO.FileAccess -> Microsoft.VisualStudio.Tools.Applications.ServerDocument
Public Sub New (documentPath As String, access As FileAccess)

參數

documentPath
String

要載入之文件的完整路徑。

access
FileAccess

表示文件檔案存取的值。

例外狀況

documentPath 參數為 null,或空字串,或整個都是空白字元。

的值 access 是 System.IO.FileAccess.Write。

documentPath 指定的檔案不存在。

參數指定的 documentPath 檔案沒有自定義專案,而的值 access 是 System.IO.FileAccess.Read。

documentPath 所指定的檔案,其自訂不是使用 Visual Studio 2010 Tools for Office Runtime 或 Visual Studio Tools for the Microsoft Office system (3.0 版執行階段) 建立。

documentPath指定的檔案具有 Visual Studio Tools for Office 運行時間不支援的擴展名。

範例

下列程式代碼範例會使用 建 ServerDocument(String, FileAccess) 構函式來建立新的 ServerDocument ,以載入具有唯讀存取權的指定檔。 然後,程式代碼會顯示附加至檔之自定義的部署指令清單 URL。

這個範例需要:

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

  • 下列元件的參考:

    • 如果專案以 .NET Framework 4 或 .NET Framework 4.5) 為目標,則 Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.dll (。

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

  • Imports (visual Basic) 或 using C#) 語句 Microsoft.VisualStudio.Tools.Applications 的 (和 Microsoft.VisualStudio.Tools.Applications.Runtime 程式代碼檔案頂端的 命名空間。

private void CreateServerDocumentReadOnly(string documentPath)
{
    int runtimeVersion = 0;
    ServerDocument serverDocument1 = null;

    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
        if (runtimeVersion == 3)
        {
            serverDocument1 = new ServerDocument(documentPath,
                System.IO.FileAccess.Read);
            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 (DocumentNotCustomizedException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document does not " +
            "have a customization.");
    }
    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();
    }
}
Private Sub CreateServerDocumentReadOnly(ByVal documentPath As String)
    Dim runtimeVersion As Integer = 0
    Dim serverDocument1 As ServerDocument = Nothing

    Try
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
        If runtimeVersion = 3 Then
            serverDocument1 = New ServerDocument(documentPath, System.IO.FileAccess.Read)
            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 DocumentNotCustomizedException
        System.Windows.Forms.MessageBox.Show("The specified document does not " & _
            "have a customization.")
    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
    End Try
End Sub

備註

如果您想要以唯讀或唯讀存取權開啟檔,請使用這個建構函式來存取磁碟上的檔快取數據或部署指令清單資訊。 根據預設,其他 ServerDocument 建構函式會開啟具有讀取/寫入存取權的檔。

適用於