试图加载自定义配置时FileNotFoundException异常在Visual Studio安装和部署项目自定义、加载、异常、项目

2023-09-04 22:59:08 作者:  (-在乎谁。 | (-遗忘谁。

我想打电话给我设置和放大器中的自定义操作;部署计划来更新我的应用程序在app.config中的一些项目。我已经结束了在通常的方式自定义配置部分,例如:

I'm trying to call a custom action within my Setup & Deployment project to update some items within the app.config on my app. I've wrapped up the custom config section in the usual way, e.g.:

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

我给自己定的安装,之后base.Install(stateSaver)安装节期间,被称为自定义操作。在code是:

I've set the custom action to be called during the Install section of installation just after base.Install(stateSaver). The code is:

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");

当我运行它,我得到这个错误:

When I run this, I get this error:

System.Configuration.ConfigurationErrorsException:出错创造MyApp的配置节处理程序:无法加载文件或程序集MyCompany的。 MyApp.Configuration或它的某一个依赖。该系统找不到指定的文件。 (C:\ Program Files文件\ MyCompany的\ MyApp的\ MyApp.exe.config 5号线)---> System.IO.FileNotFoundException:找不到无法加载文件或程序集MyCompany.MyApp.Configuration或它的某一个依赖。该系统找不到指定的文件。

System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for MyApp: Could not load file or assembly 'MyCompany. MyApp.Configuration' or one of its dependencies. The system cannot find the file specified. (C:\Program Files\MyCompany\MyApp\MyApp.exe.config line 5) ---> System.IO.FileNotFoundException: Could not load file or assembly 'MyCompany.MyApp.Configuration' or one of its dependencies. The system cannot find the file specified.

5号线的配置为:

<section name="MyApp"
    type="MyCompany.MyApp.Configuration.MyApp, MyCompany.MyApp.Configuration"
    requirePermission="false" />

该类库的安装程序code(这是该库中的唯一一类)有一个参考配置组件。

The class library with the installer code (that being the only class in that library) has a reference to the config assembly.

有什么真的很明显,我在这里失踪?为什么没有被发现裁判的配置我不能工作了。

Is there something really obvious that I'm missing here? I can't work out why the ref to the config isn't being found.

任何帮助/建议将是非常美联社preciated。

Any help/suggestions would be very much appreciated.

推荐答案

我设法找到MSDN上的一些code,它提供了一个工作(虽然黑客攻击)的方式来做到这一点。链接是在这里:ConfigurationManager,自定义的配置,和installutil /路径搜索问题。

I managed to find some code on MSDN that provides a working (albeit hacked) way to do this. Link is here: ConfigurationManager, custom config, and installutil / path searching problem.

会不会发现它,如果不是能与帮助调试的建议,以便得益于双方你。

Wouldn't have found it if not for being able to debug with help from suggestions so thanks to both of you for that.

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

For reference, my final install code is:

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;
    }

}
 
精彩推荐
图片推荐