2009-08-24 46 views
4

我正在嘗試在自定義操作中安裝產品期間更新web.config文件的自定義配置節。但是,我想使用實際的配置類來執行此操作,但安裝程序在運行它時會加載我的安裝程序類,但隨後會嘗試從Windows系統目錄加載我的自定義節類,因此Configuration.GetSection會拋出File Not Found異常。我設法通過將所需的程序集複製到Windows系統目錄中來實現此目的,但這不是一個理想的解決方案,因爲我無法保證我總能訪問該目錄。在安裝程序類中編輯自定義配置節

我該怎麼解決這個問題?

我更新的代碼如下所示

[RunInstaller(true)] 
public partial class ProjectInstaller : Installer 
{ 
    public override void Install(System.Collections.IDictionary stateSaver) 
    { 
     //some code here 
     webConfig = WebConfigurationManager.OpenWebConfiguration("MyService"); 
     MyCustomSection mySection = webconfig.GetSection("MyCustomSection") //<--File Not Found: CustomConfigSections.dll 
     //Update config section and save config 
    } 
} 

我的配置文件看起來像這樣

<configuration> 
    <configSections> 
     <section name="myCustomSection" type="CustomConfigSections.MyCustomSection, CustomConfigSections" /> 
    </configSections> 
    <myCustomSection> 
     <!-- some config here --> 
    </myCustomSection> 
</configuration> 
+0

你解決了嗎?我有同樣的問題。我可以訪問AppSettings,但是加載ConfigHandler所需的DLL是安裝的一部分,雖然看起來存在於文件夾中,但無法訪問。 我認爲這可能有幫助,但目前爲止沒有運氣: AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.AssemblyResolve + = new ResolveEventHandler(MyResolveEventHandler); – Junto 2010-03-04 13:32:54

+0

我確實找到了另一個更好的解決方案。您可以使用Orca編輯msi來更改其執行的順序,以便在自定義操作運行之前部署程序集並將其放入GAC中。仍然不理想,但比以前更好。 – 2010-03-05 09:52:38

回答

1

希望你能理解答案意圖的方式。

假設您已經設置了安裝程序以使您的項目輸出。如果不是 右鍵單擊安裝程序項目單擊添加 - >項目輸出 - >選擇您的項目 然後您可以繼續使用您的代碼。

而且,如果你使用的是除了.NET問鼎DLL如果你想閱讀安裝使用BeforeInstall事件 處理程序之前的元素,並嘗試閱讀您的文件,一定要更改有 屬性copylocal =真

。 ihope您的問題將得到解決

如果萬一你想閱讀安裝後的元素右鍵單擊 安裝項目,然後單擊視圖 - > customActions->上安裝單擊添加自定義操作 - >選擇應用程序文件夾 - >選擇主從您的項目輸出並點擊 ok。

單擊主輸出,然後按F4和自定義操作的數據寫

/DIR="[TARGETDIR]\" 

,之後編寫代碼如下。

[RunInstaller(true)] 
public class ProjectInstaller : Installer 
{ 
    public ProjectInstaller() 
    { 
    this.InitializeComponent(); 
    } 
    private void InitializeComponent() 
    { 
    this.AfterInstall += new InstallEventHandler(ProjectInstaller_AfterInstall); 
    } 
    void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e) 
    { 
    string path = this.Context.Parameters["DIR"] + "YourFileName.config"; 
    // make sure you replace your filename with the filename you actually 
    // want to read 
    // Then You can read your config using XML to Linq Or you can use 
    // WebConfigurationManager whilst omitting the .config from the path 
    }