尝试加载自定义配置时,Visual Studio安装和部署项目中的FileNotFoundException

Kev*_*son 3 .net installer configuration setup-project

我正在尝试在我的安装和部署项目中调用自定义操作来更新我的应用程序上的app.config中的一些项目.我以通常的方式包装了自定义配置部分,例如:

[ConfigurationProperty("serviceProvider", IsRequired = true)]
public string ServiceProvider
{
    get { return (string)base["serviceProvider"]; }
    set { base["dataProviderFactory"] = value; }
}
Run Code Online (Sandbox Code Playgroud)

我已经在base.Install(stateSaver)之后的安装的安装部分中设置了要调用的自定义操作.代码是:

string exePath = string.Format("{0} MyApp.exe", Context.Parameters["DP_TargetDir"]);
SysConfig.Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
Configuration. MyApp section = Configuration.MyApp)config.GetSection("MyApp");
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我收到此错误:

System.Configuration.ConfigurationErrorsException:为MyApp创建配置节处理程序时发生错误:无法加载文件或程序集'MyCompany.MyApp.Configuration'或其依赖项之一.该系统找不到指定的文件.(C:\ Program Files\MyCompany\MyApp\MyApp.exe.config第5行)---> System.IO.FileNotFoundException:无法加载文件或程序集"MyCompany.MyApp.Configuration"或其依赖项之一.该系统找不到指定的文件.

配置中的第5行是:

<section name="MyApp"
    type="MyCompany.MyApp.Configuration.MyApp, MyCompany.MyApp.Configuration"
    requirePermission="false" />
Run Code Online (Sandbox Code Playgroud)

带有安装程序代码的类库(该库中唯一的类)具有对配置程序集的引用.

我有什么特别明显的东西在这里失踪吗?我无法弄清楚为什么没有找到配置的引用.

任何帮助/建议将非常感谢.

Kev*_*son 7

我设法在MSDN上找到一些代码,提供了一种工作(尽管是被黑客入侵)的方法.链接在这里:ConfigurationManager,自定义配置和installutil /路径搜索问题.

如果没有能够在建议的帮助下进行调试就不会发现它,所以感谢你们两位.

作为参考,我的最终安装代码是:

public override void Install(IDictionary stateSaver)
{
    base.Install(stateSaver);

    string targetDirectory = Context.Parameters["DP_TargetDir"];
    stateSaver.Add("TargetDir", targetDirectory);

    string exePath = Path.Combine(targetDirectory, "MyApp.exe");

    System.Diagnostics.Debugger.Break();

    ResolveEventHandler tempResolveEventHandler =
        (sender, args) =>
            {
                string simpleName = new AssemblyName(args.Name).Name;
                string path = Path.Combine(targetDirectory, simpleName + ".dll");
                return File.Exists(path)
                    ? Assembly.LoadFrom(path)
                    : null;
            };

    try
    {
        // hook up asm resolve handler  
        AppDomain.CurrentDomain.AssemblyResolve += tempResolveEventHandler;

        System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
        Configuration.MyApp section = (Configuration.MyApp) config.Sections["MyApp"];

        if (section != null)
        {
            // "ApplicationSettings.DefaultDatabasePath" is the custom config value I'm updating
            section.ApplicationSettings.DefaultDatabasePath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
            config.Save();
        }
    }
    finally
    {
        // remove asm resolve handler.  
        AppDomain.CurrentDomain.AssemblyResolve -= tempResolveEventHandler;
    }

}
Run Code Online (Sandbox Code Playgroud)