4

我正在尝试在自定义操作中安装我的产品期间更新 web.config 文件的自定义配置部分。我想使用实际的配置类来执行此操作,但是当安装程序运行时,它会加载我的安装程序类,但是 Configuration.GetSection 会在尝试从 Windows 系统目录加载我的自定义部分类时抛出 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>
4

1 回答 1

1

希望您能按照预期的方式理解答案。

假设您已将安装程序设置为具有项目输出。如果不是右键单击安装程序项目单击添加->项目输出->选择您的项目,然后您可以继续使用您的代码。

此外,如果您使用的是 .net 以外的 dll,请确保将那里的属性更改为 copylocal = true

如果您想在安装之前阅读元素,请使用 BeforeInstall 事件处理程序并尝试阅读您的文件。希望你的问题能得到解决

如果您想在安装后读取元素右键单击安装程序项目单击视图->自定义操作->安装时单击添加自定义操作->选择应用程序文件夹->从项目中选择主要输出,然后单击确定。

现在单击主输出并按 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
  }
于 2013-02-18T17:59:12.990 回答