2

構成ファイルに値を追加した後、すぐにアプリケーションに読み込めない理由を誰かが理解するのを手伝ってくれますか? リフレッシュしますが、うまくいきません。下記参照:

    public void AddConfig(string key_value, string actual_value)
    {
        // Open App.Config of executable
        System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
        //If it exists, remove it first
        config.AppSettings.Settings.Remove(key_value);
        // Add an Application Setting.
        config.AppSettings.Settings.Add(key_value, actual_value);            
        // Save the configuration file.
        config.Save(ConfigurationSaveMode.Modified);
        // Force a reload of a changed section.            
        ConfigurationManager.RefreshSection("appSettings");
        string blah = ConfigurationManager.AppSettings[key_value];
        MessageBox.Show(blah);
    }

メッセージ ボックスは null/空白として表示されます。

アプリケーションを再起動し、別のコマンドを実行して起動後にキー値を読み取ると、機能します。

何か案は?

4

1 に答える 1

1

あなたが経験していることは、Visual Studio 環境内からアプリケーションを実行していることが原因である可能性が非常に高いです。

DebugorReleaseディレクトリから .exe を直接実行すると、RefreshSection()メソッドは期待どおりに機能します。

デバッグ中に変更された値を確認したい場合は、 AppSettings.Settings代わりにConfigurationManager.AppSettings次を使用する必要があります。

string blah = config.AppSettings.Settings[key_value].Value
于 2013-05-30T20:51:50.650 に答える