加载配置文件时出错,参数无效

时间:2013-12-20 06:34:59

标签: c# configuration installer string-formatting configurationmanager

我有一个安装程序类,它从Visual Studio安装项目中检索参数。现在打开exeConfiguration我收到以下错误..

Error 1001:An error occurred loading a configuration file.
The parameter 'exePath' is       invalid.
Parameter name:exepath-->The Parameter   'exePath'   is invalid.
Parameter name:exePath

这是我的Installer.cs代码..

public override void Install(System.Collections.IDictionary stateSaver)
    {

        base.Install(stateSaver);

        string targetDirectory = Context.Parameters["targetdir"];

        string param1 = Context.Parameters["Param1"];

        string param2 = Context.Parameters["Param2"];

        string param3 = Context.Parameters["Param3"];

        string exePath = string.Format("{0}TechSoft CallBill.exe", targetDirectory);

        Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);

        config.AppSettings.Settings["Param1"].Value = param1;

        config.AppSettings.Settings["Param2"].Value = param2;

        config.AppSettings.Settings["Param3"].Value = param3;

        config.Save();
    }

enter image description here

请帮我解决这个错误因为我无法弄明白。 任何建议都受到热烈欢迎。 提前致谢

1 个答案:

答案 0 :(得分:1)

如果Config文件属于同一个项目,那么试试这个,如果配置文件属于同一个项目,那么你不必使用配置文件路径。

Configuration config= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings["Param1"].Value = param1;

config.AppSettings.Settings["Param2"].Value = param2;

config.AppSettings.Settings["Param3"].Value = param3;

//Save only the modified section of the config
config.Save(ConfigurationSaveMode.Modified);

//Refresh the appSettings section to reflect updated configurations
ConfigurationManager.RefreshSection(Constants.AppSettingsNode);
相关问题