7
  • インストーラーから継承されたInstallhelper.csを作成しました。
  • このコード(A)を使用して、Install()メソッドをオーバーライドします。

app.configに挿入されるこれらの値は、インストール後に作成される[projectName].exe.configファイルでは使用できません。以下のようなセクションをapp.configに手動で追加しました(B)

データはインストーラークラスに渡されますが、データはapp.configフィールドに書き込まれません。これらは、インストール中に作成された構成ファイルで同じままです。

どんな助けでも大歓迎です。私はこれにほぼ1日を費やしました。

コード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>
4

2 に答える 2

7

このコーディングの問題はこの行でした

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

インストール中に設定ファイルが依存するパスは提供されないため、次のように変更する必要があります。

string path = Path.Combine(new DirectoryInfo(Context.Parameters["assemblypath"].ToString()).Parent.FullName, "[project name].exe")

    Configuration config = ConfigurationManager.OpenExeConfiguration(path);

今、その動作。:)

于 2012-07-06T08:01:26.503 に答える
1

ありがとうございました。この正確な問題があり、ファイルに書き込まれない理由を理解できませんでした。私がした唯一の異なることは、アプリケーションからパスを取得することでした。

string path = Application.ExecutablePath;
Configuration config = ConfigurationManager.OpenExeConfiguration(path);
于 2012-11-14T15:23:11.790 に答える