92

質問するのは些細なことに聞こえるかもしれませんが、私は記事で提案されているのと同じことをしていますが、期待どおりには機能しません。誰かが私を正しい方向に向けてくれることを願っています。

AppSettings ごとにユーザー設定を保存したいと思います。

Winform が閉じられたら、これをトリガーします。

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

if (ConfigurationManager.AppSettings["IntegrateWithPerforce"] != null)
    ConfigurationManager.AppSettings["IntegrateWithPerforce"] = 
                                           e.Payload.IntegrateCheckBox.ToString();
else
    config.AppSettings.Settings.Add("IntegrateWithPerforce", 
                                          e.Payload.IntegrateCheckBox.ToString());

config.Save(ConfigurationSaveMode.Modified);

そのため、エントリがまだ存在しない場合は、単純に作成されます。それ以外の場合は、既存のエントリが変更されます。ただし、これは保存されません。

1) 私は何を間違っていますか?

2) アプリ設定のユーザー設定が再び保存される場所はどこですか? Debug フォルダにありますか、それとも C:\Documents and Settings\USERNAME\Local Settings\Application Data フォルダにありますか?

4

9 に答える 9

94

私は遅れていることを知っています:)しかし、これは私がそれを行う方法です:

public static void AddOrUpdateAppSettings(string key, string value)
{
    try
    {
        var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        var settings = configFile.AppSettings.Settings;
        if (settings[key] == null)
        {
            settings.Add(key, value);
        }
        else
        {
            settings[key].Value = value;
        }
        configFile.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
    }
    catch (ConfigurationErrorsException)
    {
        Console.WriteLine("Error writing app settings");
    }
}

詳細については、MSDNを参照してください

于 2014-11-06T11:29:39.433 に答える
72

app.configファイルのappSettingsセクションの値を変更する方法について:

config.AppSettings.Settings.Remove(key);
config.AppSettings.Settings.Add(key, value);

仕事をします。

もちろん、より良い練習は設定クラスですが、それはあなたが何を求めているかによって異なります。

于 2011-11-29T15:52:02.470 に答える
41

セクションを優先<appSettings><customUserSetting>ます。(Web)ConfigurationManager を使用すると、読み取りと書き込みがはるかに簡単になります。ConfigurationSection、ConfigurationElement、および ConfigurationElementCollection では、カスタム クラスを派生させ、カスタム ConfigurationProperty プロパティを実装する必要があります。単なる日常の人間の IMO には多すぎます。

web.config の読み取りと書き込みの例を次に示します。

using System.Web.Configuration;
using System.Configuration;

Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
string oldValue = config.AppSettings.Settings["SomeKey"].Value;
config.AppSettings.Settings["SomeKey"].Value = "NewValue";
config.Save(ConfigurationSaveMode.Modified);

前:

<appSettings>
  <add key="SomeKey" value="oldValue" />
</appSettings>

後:

<appSettings>
  <add key="SomeKey" value="newValue" />
</appSettings>
于 2012-04-12T11:47:53.677 に答える
26

おそらく、設定ファイルの追加を検討する必要があります。(例: App.Settings) このファイルを作成すると、次のことが可能になります。

string mysetting = App.Default.MySetting;
App.Default.MySetting = "my new setting";

これは、項目が厳密に型指定されている項目を編集してから変更できることを意味します。何よりも、展開する前に xml に触れる必要はありません。

結果は、アプリケーションまたはユーザーのコンテキスト設定です。

設定ファイルの「新しい項目の追加」メニューを見てください。

于 2011-03-11T15:38:25.543 に答える
23

基本的な質問は勝利フォームに関するものであるため、ここに解決策があります:( user1032413 によるコードを rflect windowsForms 設定に変更したところです)それが新しいキーの場合:

Configuration config = configurationManager.OpenExeConfiguration(Application.ExecutablePath); 
config.AppSettings.Settings.Add("Key","Value");
config.Save(ConfigurationSaveMode.Modified);

キーがすでに存在する場合:

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath); 
config.AppSettings.Settings["Key"].Value="Value";
config.Save(ConfigurationSaveMode.Modified);
于 2013-12-25T13:25:08.650 に答える
10

保存呼び出しの後にこれを追加してみてください。

ConfigurationManager.RefreshSection( "appSettings" );
于 2011-03-11T17:31:26.967 に答える
6

ConfigurationManagerは1つのapp.config(スタートアッププロジェクトにあるもの)のみを使用することに注意してください。

あるapp.configをソリューションAに配置し、別のソリューションBからそれを参照する場合、Bを実行すると、Aからのapp.configは無視されます。

したがって、たとえば、単体テストプロジェクトには独自のapp.configが必要です。

于 2012-12-15T01:06:34.193 に答える