2009-08-24 42 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 
    }