ServerDocument 類別

提供文件或活頁簿中存取快取資料和自訂資訊的方式,這些快取資料和自訂資訊是使用 Visual Studio 的 Office 開發工具所建立的文件層級自訂的一部分。

繼承階層架構

System.Object
  Microsoft.VisualStudio.Tools.Applications.ServerDocument

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

語法

'宣告
<PermissionSetAttribute(SecurityAction.Demand, Name := "FullTrust")> _
Public NotInheritable Class ServerDocument _
    Implements IDisposable
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
public sealed class ServerDocument : IDisposable

ServerDocument 型別會公開下列成員。

建構函式

  名稱 說明
公用方法 ServerDocument(String) 使用要載入之文件的完整路徑,初始化 ServerDocument 類別的新執行個體。
公用方法 ServerDocument(array<Byte[], String) 使用位元組陣列和文件的副檔名,初始化 ServerDocument 類別的新執行個體,位元組陣列表示要載入的文件。
公用方法 ServerDocument(Stream, String) 使用資料流和文件副檔名,初始化 ServerDocument 類別的新執行個體,資料流表示要載入的文件。
公用方法 ServerDocument(String, FileAccess) 使用要載入之文件的完整路徑以及表示文件檔案存取的值,初始化 ServerDocument 類別的新執行個體。

回頁首

屬性

  名稱 說明
公用屬性 CachedData 取得 CachedData 物件,表示文件中包含的快取資料。
公用屬性 DeploymentManifestUrl 取得或設定與文件相關聯之自訂的部署資訊清單 URL。
公用屬性 Document 取得載入至 ServerDocument 的記憶體中文件的位元組陣列。
公用屬性 SolutionId 取得 Visual Studio Tools for Office Runtime 用來識別方案的 GUID。

回頁首

方法

  名稱 說明
公用方法靜態成員 AddCustomization(String, Uri) 使用指定的組件名稱和部署資訊清單,將自訂附加至指定的文件。
公用方法靜態成員 AddCustomization(String, String, Guid, Uri) 使用指定的文件、組件名稱、方案 ID 和部署資訊清單,將自訂附加至指定的文件。
公用方法靜態成員 AddCustomization(String, String, Guid, Uri, Boolean, array<String[]%) 基礎架構。
公用方法 Close 關閉 ServerDocument 物件。
公用方法 Equals 判斷指定的 Object 和目前的 Object 是否相等。 (繼承自 Object)。
受保護的方法 Finalize 允許物件在記憶體回收進行回收之前,嘗試釋放資源並執行其他清除作業。 (繼承自 Object)。
公用方法靜態成員 GetCustomizationVersion 傳回 Visual Studio Tools for Office Runtime 的版本,與指定文件關聯的自訂是使用此版本建立的。
公用方法 GetHashCode 做為特定型別的雜湊函式。 (繼承自 Object)。
公用方法 GetType 取得目前執行個體的 Type。 (繼承自 Object)。
公用方法靜態成員 IsCacheEnabled 取得值,指出指定的文件是否含有資料快取。
公用方法靜態成員 IsCustomized 取得值,這個值表示指定的文件是否含有使用 Visual Studio 2010 Tools for Office Runtime 所建立的自訂。
受保護的方法 MemberwiseClone 建立目前 Object 的淺層複本 (Shallow Copy)。 (繼承自 Object)。
公用方法靜態成員 RemoveCustomization 從文件中移除自訂。
公用方法 Save 使用 ServerDocument 類別,儲存對文件所做的任何變更。
公用方法 ToString 傳回表示目前物件的字串。 (繼承自 Object)。

回頁首

明確介面實作

  名稱 說明
明確介面實作私用方法 IDisposable.Dispose 釋放 ServerDocument 使用的所有資源。

回頁首

備註

在未安裝 Excel 或 Word 的電腦上,使用 ServerDocument 類別來管理文件層級自訂的某些方面。 通常您會在未與 Office 整合的應用程式 (例如主控台專案或 Windows Form 專案,而不是 Office 專案) 中使用這個類別。

使用 ServerDocument 類別的下列成員執行一般工作:

  • 若要存取及修改伺服器上文件資料快取中的資料,請使用 CachedData 屬性。

  • 若要以程式設計的方式在文件中附加自訂或移除自訂,請使用 AddCustomizationRemoveCustomization 方法。

  • 若要存取或變更與文件關聯之部署資訊清單的 URL,請使用 DeploymentManifestUrl 屬性。

Visual Studio 2010 Tools for Office Runtime 中有兩種不同版本的 ServerDocument 類別。 您應該使用的版本是根據要使用該類別的應用程式中的目標 .NET Framework 而定:

  • 針對目標為 .NET Framework 4 的應用程式,使用 Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 組件中的 Microsoft.VisualStudio.Tools.Applications.ServerDocument 類別。

  • 針對目標為 .NET Framework 3.5 的應用程式,使用 Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll 組件中的 Microsoft.VisualStudio.Tools.Applications.ServerDocument 類別。

如需詳細資訊,請參閱使用 ServerDocument 類別管理伺服器上的文件

選擇要使用的建構函式

若要使用 ServerDocument 類別存取文件中的快取資料或部署資訊清單 URL,您必須建立 ServerDocument 物件。

有兩組 ServerDocument 建構函式:

  • 一組可用來存取已在記憶體中開啟的文件。

  • 一組可用來存取磁碟上的文件。

存取記憶體中的文件

若要存取已在記憶體中開啟的文件,請使用下列其中一個建構函式:

這些建構函式可接受表示記憶體中之文件的位元組陣列或 Stream。 如果您要修改文件中的快取資料或應用程式資訊清單,然後再使用 HTTP 通訊協定將文件資料流處理至目的端,後者相當實用。 若要使用這些建構函式,文件必須已經有自訂,否則建構函式會擲回 CannotLoadManifestException 例外狀況。

存取磁碟上的文件

若要存取磁碟上的文件,請使用下列其中一個建構函式:

這些建構函式可接受要開啟之文件的完整路徑。 根據預設,文件是利用讀取/寫入存取權開啟的。 如果您要以唯讀或唯寫存取權開啟文件,請使用具有 FileAccess 參數的建構函式。

範例

下列程式碼範例會建立新的 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 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
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();
    }
}

執行緒安全

這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。

請參閱

參考

Microsoft.VisualStudio.Tools.Applications 命名空間

其他資源

使用 ServerDocument 類別管理伺服器上的文件

Office 方案中的應用程式和部署資訊清單

存取伺服器文件中的資料

HOW TO:將資料插入伺服器上的活頁簿中

HOW TO:從伺服器的活頁簿中擷取快取資料

HOW TO:變更伺服器活頁簿中的快取資料