2017-05-15 1 views
1

私はapp.configファイルを編集して保存するプログラムを持っています。私は編集して保存することができますが、アプリケーションを再起動せずにapp.configデータをリロードすることはできません。 私はそれは私の古い設定を与える保存ボタンをクリックしたときに、私は...これは私のコードであるapp.configファイルのデータを再ロードするには

間違っているかわからない

private void btnSave_Click(object sender, EventArgs e) 
{    
    UpdateConfig("IP",radtxtIP.Text, "myApp.exe"); 
    UpdateConfig("port", radtxtPort.Text, "myApp.exe"); 
    LoadConfigData(); 
} 
private void UpdateConfig(string key, string value, string fileName) 
{ 
    var configFile = ConfigurationManager.OpenExeConfiguration(fileName); 
    configFile.AppSettings.Settings[key].Value = value; 
    configFile.Save(); 
} 

public void LoadConfigData() 
{ 
     ConfigurationManager.RefreshSection("appSettings"); 
     Properties.Settings.Default.Reload(); 
     radtxtIP.Text = ConfigurationManager.AppSettings["IP"]; 
     radtxtPort.Text = ConfigurationManager.AppSettings["port"]; 
} 

をways.i'mのカップルを試してみました。 私はstackoverflowの中でいくつかの質問を読んだ後、このコードを追加しましたが、全く使用

ConfigurationManager.RefreshSection("appSettings"); 
Properties.Settings.Default.Reload(); 

は私がのConfigurationManagerの新しいインスタンスを作成する必要はないのですか? それは私が設定データを使用する方法ですか? 私のアプリは、多くの場所でapp.configデータを使用します。そのため、設定データを変更するときにアプリを再起動する必要があります。

あなたはこの参考になっを見つけるかもしれない

答えて

1

下に示すように、新しい設定をロードしてください私はこのようないくつかのものを使用し、この1簡単

class SettingsService 
{ 

    public static string GetSetting(string key) 
    { 
     System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
     return config.AppSettings.Settings[key].Value.ToString(); 
    } 

    public static void UpdateConfig(string key, string value) 
    { 
     var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
     configFile.AppSettings.Settings[key].Value = value; 
     configFile.Save(ConfigurationSaveMode.Modified); 
    } 
} 
を期待してください。

UpdateConfig("key1","value")メソッド を呼び出して設定を更新でき、このを使用して設定を取得できます

注: - 使用VSは、アプリケーションを実行するにはそれだけで

0


https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.refreshsection.aspx

// Get the AppSettings section. 
AppSettingsSection appSettingSection = (AppSettingsSection)config.GetSection(sectionName); 
+0

それは本当に助け、あなたが答えてくれてありがとうアップデートApplicationName.vshost.exe(設定ファイル)です。 'appSettingsSection appSettingSection =(AppSettingsSection)config.GetSection(sectionName)'しかし、このコードはこのようなものをいくつか与えます ' 私は別々に値をキーしたい –

関連する問題