當您先前在此逐步解說中按下 F5 來執行專案時,此建置處理序會將活頁簿中內嵌的應用程式資訊清單編輯成指向組件的相對路徑。如果活頁簿和組件在安裝後仍然保留在相同的資料夾中,您就不需要修改內嵌的應用程式資訊清單,而且可以忽略本節。不過,如果您不想要讓使用者在安裝方案後將活頁簿移至不同的資料夾,則必須將應用程式資訊清單編輯成指向組件的完整路徑。
您必須在安裝程序完成後執行自訂動作,以便更新內嵌於 Visual Studio Tools for Office 方案文件中的應用程式資訊清單,因為方案組件的位置要到使用者在安裝時指定位置才會知道。您可以使用 ServerDocument 類別,編輯內嵌的應用程式資訊清單。若要在安裝專案中使用 ServerDocument 類別,請將程式碼加入至自訂動作專案內的 Installer 類別。
若要建立可編輯應用程式資訊清單的自訂動作
在 [方案總管] 中的 ExcelCustomAction 專案上按一下滑鼠右鍵。
在快速鍵功能表上指向 [加入],再按一下 [新增項目]。
[加入新項目] 對話方塊隨即出現。
選取 [安裝程式類別],然後將類別命名為 ManifestEditor。
將 Microsoft.VisualStudio.Tools.Applications.Runtime 組件的參考加入至 ExcelCustomAction 專案中。
在 [方案總管] 中,以滑鼠右鍵按一下 ManifestEditor.cs 或 ManifestEditor.vb 檔,然後按一下 [檢視程式碼]。
在程式碼檔的頂端加入下列 Imports 或 using 陳述式。
Imports Microsoft.VisualStudio.Tools.Applications.Runtime
using Microsoft.VisualStudio.Tools.Applications.Runtime;
將下列程式碼複製到 ManifestEditor 類別中。
這段程式碼會覆寫 Install 方法,而此方法是用來執行自訂安裝動作。此程式碼會將使用者指定的安裝位置設定為 AssemblyPath 屬性。使用者指定的安裝路徑以及文件和組件的名稱都是從 Parameters 屬性取得。
Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
UpdateApplicationManifest()
MyBase.Install(stateSaver)
End Sub
Private Sub UpdateApplicationManifest()
' Get the parameters passed to the task.
Dim targetDir As String = Me.Context.Parameters("targetdir")
Dim documentName As String = Me.Context.Parameters("documentname")
Dim assemblyName As String = Me.Context.Parameters("assemblyname")
' Get the application manifest from the document.
Dim documentPath As String = System.IO.Path.Combine(targetDir, documentname)
Dim serverDocument1 As ServerDocument = New ServerDocument(documentPath, _
System.IO.FileAccess.ReadWrite)
Try
Dim appManifest1 As AppManifest = serverDocument1.AppManifest
Dim assemblyPath As String = System.IO.Path.Combine(targetDir, assemblyName)
appManifest1.Dependency.AssemblyPath = assemblyPath
serverDocument1.Save()
Finally
If Not serverDocument1 Is Nothing Then
serverDocument1.Close()
End If
End Try
End Sub
// Override the Install method to update the customization location
// in the application manifest.
public override void Install(System.Collections.IDictionary stateSaver)
{
UpdateApplicationManifest();
base.Install(stateSaver);
}
// Update the application manifest according to the the install location.
private void UpdateApplicationManifest()
{
// Get the parameters passed to the task.
string targetDir = this.Context.Parameters["targetdir"];
string documentName = this.Context.Parameters["documentname"];
string assemblyName = this.Context.Parameters["assemblyname"];
// Get the application manifest from the document.
string documentPath = System.IO.Path.Combine(targetDir, documentName);
ServerDocument serverDocument1 = new ServerDocument(documentPath,
System.IO.FileAccess.ReadWrite);
try
{
AppManifest appManifest1 = serverDocument1.AppManifest;
string assemblyPath = System.IO.Path.Combine(targetDir, assemblyName);
appManifest1.Dependency.AssemblyPath = assemblyPath;
serverDocument1.Save();
}
finally
{
if (serverDocument1 != null)
{
serverDocument1.Close();
}
}
}
在 [方案總管] 中以滑鼠右鍵按一下 ExcelCustomAction 專案,然後按一下 [建置]。