Как записать в app.config в проекте установки и использовать его в программе

  • Я создал файл Installhelper.cs, унаследованный от установщика.
  • Переопределите метод Install(), используя этот фрагмент кода (A).

Эти значения, которые вставляются в app.config, недоступны в файле [projectName].exe.config, который создается после установки. Я уже добавил раздел, как показано ниже, в app.config вручную (B)

данные передаются в класс установщика, но данные не записываются в поля app.config. Они остаются такими же в созданном файле конфигурации во время установки.

Любая помощь приветствуется. Я потратил на это почти сутки.

Код A:

[RunInstaller(true)]
public partial class Installation : System.Configuration.Install.Installer
{
    public Installation()
    {
        InitializeComponent();
    }

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

        try
        {
            // In order to get the value from the textBox named 'EDITA1' I needed to add the line:
            // '/PathValue = [EDITA1]' to the CustomActionData property of the CustomAction We added. 

            string userName = Context.Parameters["userName"];
            string password = Context.Parameters["password"];
            string folderPath = Context.Parameters["path"];

            MessageBox.Show(userName);
            MessageBox.Show(password);
            MessageBox.Show(folderPath);

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
           //config.AppSettings.Settings.Add("userName", userName);
           //config.AppSettings.Settings.Add("password", password);
           //config.AppSettings.Settings.Add("foderPath", folderPath);





            config.AppSettings.Settings["userName"].Value = userName;
            config.AppSettings.Settings["password"].Value = password;
            config.AppSettings.Settings["foderPath"].Value = folderPath;
            config.Save(ConfigurationSaveMode.Full);
            ConfigurationManager.RefreshSection("appSettings");




        }
        catch (FormatException e)
        {
            string s = e.Message;
            throw e;
        }
    }
}

В конфигурацию приложения добавлен раздел Код B:

<appSettings>
<add key="userName" value="" />
<add key="password" value="" />
<add key="foderPath" value="" />
</appSettings>
7
задан diyoda_ 28 June 2012 в 03:34
поделиться