2012-06-22 3 views
34

これは、このコードでは、私のapp.configをapp.configを変更値

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <appSettings> 
    <add key="lang" value="English"/> 
    </appSettings> 
</configuration> 

である私は、変更

lang = "Russian"; 
private void Main_FormClosing(object sender, FormClosingEventArgs e) 
{ 
    System.Configuration.ConfigurationManager.AppSettings.Set("lang", lang); 
} 

を作るしかし、それは変わりません。私は間違っているの?

+0

も参照してください:http://stackoverflow.com/questions/5274829/configurationmanager-appsettings-how-to-modify-and-保存 –

+0

[app.configファイルの値を書き込む]の可能な複製(http://stackoverflow.com/questions/4758598/write-values-in-app-config-file) – nawfal

答えて

41

AppSettings静的オブジェクトを使用することはできません。試してみてください

string appPath = System.IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location);   
string configFile = System.IO.Path.Combine(appPath, "App.config"); 
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();   
configFileMap.ExeConfigFilename = configFile;   
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None); 

config.AppSettings.Settings["YourThing"].Value = "New Value"; 
config.Save(); 
+2

しかし、それは再起動します...私は変更を行うonフォームを閉じる...私は何もプログラムをもう一度実行すると何も変わらない – a1204773

+0

あなたは例外を受け取りますか? WinFormsを使用していますか? – fenix2222

+0

はい私はWinFormsを使用しています...例外はありません... – a1204773

82

AppSettings.Setあなたの設定ファイルに変更が残っていません。それは単にメモリ内で変更されます。 System.Configuration.ConfigurationManager.AppSettings.Set("lang", lang);にブレークポイントを設定し、System.Configuration.ConfigurationManager.AppSettings[0]の時計を追加すると、そのコード行が実行されるときに "英語"から "ロシア語"に変更されます。

(コンソールアプリケーションで使用される)次のコードは、変更を持続します。このポストから

class Program 
{ 
    static void Main(string[] args) 
    { 
     UpdateSetting("lang", "Russian"); 
    } 

    private static void UpdateSetting(string key, string value) 
    { 
     Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
     configuration.AppSettings.Settings[key].Value = value; 
     configuration.Save(); 

     ConfigurationManager.RefreshSection("appSettings"); 
    } 
} 

http://vbcity.com/forums/t/152772.aspx

一つの大きなポイント以上で注意すべきは、あなたが(Visual Studioの内)デバッガからこれをその後、app.configファイルがします実行している場合ということですあなたがビルドするたびに上書きされます。これをテストする最善の方法は、アプリケーションをビルドして出力ディレクトリに移動し、そこから実行可能ファイルを起動することです。出力ディレクトリ内には、設定ファイルであるYourApplicationName.exe.configという名前のファイルもあります。これをメモ帳で開き、変更が実際に保存されていることを確認します。

+0

どのように変更できますか? – a1204773

+0

System.Configuration.ConfigurationManager.AppSettings [0]は単独では何もしません...コード – a1204773

+0

を記入してください実際に@Loclip、 'AppSettings [0]'はコレクションの最初の項目を返すべきです。この場合は ' langのエントリ。 –

2
private static string GetSetting(string key) 
    { 
     return ConfigurationManager.AppSettings[key]; 
    } 

    private static void SetSetting(string key, string value) 
    { 
     Configuration configuration = 
      ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
     configuration.AppSettings.Settings[key].Value = value; 
     configuration.Save(ConfigurationSaveMode.Full, true); 
     ConfigurationManager.RefreshSection("appSettings"); 
    } 
+1

ConfigurationSaveMode.Fullに設定を保存したくない場合があります。これはあなたのapp.configにたくさんのものをあなたが望まないものにします。残りは良いです。 – Jeremy

+0

@Amin Ghaderiの答え – mcw0933

10

デバッグフォルダにnameyourapp.exeでクリックしたときに使用「ConfigurationUserLevel.None」あなたのコードは、右実行されると。 。
しかし、あなたの視覚的なstdio上でアプリを開発するときは、正しく動作しません! "vshost.exe"が実行されているためです。パラメータ次

は、この問題を解決: "Application.ExecutablePath"

これを試してみてください(デスクトップ用VS 2012のExpressでテスト済み)

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath); 
config.AppSettings.Settings["PortName"].Value = "com3"; 
config.Save(ConfigurationSaveMode.Minimal); 

良い私の英語ではありません、 ごめんなさい。

1

.NET 4.0コンソールアプリケーションの場合、これらのどれも私にとっては役に立たなかった。だから私は、以下のようKevn Aenmeyの答えを修正し、それが働いた:

private static void UpdateSetting(string key, string value) 
{ 
    Configuration configuration = ConfigurationManager. 
     OpenExeConfiguration(Assembly.GetExecutingAssembly().Location); 
    configuration.AppSettings.Settings[key].Value = value; 
    configuration.Save(); 

    ConfigurationManager.RefreshSection("appSettings"); 
} 

のみ最初の行は、実際の実行時にアセンブリを構築、異なっています。

2

私は最後の行を変更する必要がありました(私のために働いた)fenix2222によって解答に加えて:これがなけれ

config.Save(ConfigurationSaveMode.Modified); 

、新しい値がまだ設定ファイルに書き込まれますが、古いされていましたデバッグ時に値が取得されました。

string configPath = Path.Combine(System.Environment.CurrentDirectory, "YourApplication.exe"); 
Configuration config = ConfigurationManager.OpenExeConfiguration(configPath); 
config.AppSettings.Settings["currentLanguage"].Value = "En"; 
config.Save(); 
5

。 私のために適切に働いた。

値を読み込み、文字列を返す別の有用なコードスニペット:

public static string ReadSetting(string key) 
    { 
     System.Configuration.Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
     System.Configuration.AppSettingsSection appSettings = (System.Configuration.AppSettingsSection)cfg.GetSection("appSettings"); 
     return appSettings.Settings[key].Value; 

    } 
+2

で述べたように、 'OpenExeConfiguration'を呼び出すための' Application.ExecutablePath'オプションと組み合わせて、あなたのコードもうまくいくようにしたいかもしれません。ありがとう! – jned29

+1

@ jned29あなたに役立つなら、私の答えをupvote自由に感じてください:)。 – StepUp

0

おかげJahmicの答え:WPFアプリケーションで私のために働い

関連する問題