2012-06-08 194 views
0

我正試圖在不關閉應用程序的情況下更新配置文件。問題是即時通訊仍在閱讀緩存版本。我有一個不起作用的FileSystemWatcher。任何幫助表示讚賞更新配置文件,無需重新啓動應用程序

public partial class ChangeURL : Form 
{ 

    Service ser = new Service(); 
    Configuration config = 
     ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 



    public ChangeURL() 
    { 
     InitializeComponent(); 
     textBox1.Text = ser.Url; 
     start(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     changeSettings(); 
     this.Close(); 
    } 
    public void changeSettings() 
    { 

     KeyValueConfigurationCollection settings = config.appSettings.Settings; 

     try 
     { 
      ConfigurationManager.RefreshSection("appSettings"); 
      settings["client_postCodeRef_Service"].Value = textBox1.Text; 
      ser.Url = settings["client_postCodeRef_Service"].Value; 
      config.Save(ConfigurationSaveMode.Modified); 


     } 
     catch (ConfigurationErrorsException e) 
     { 
      MessageBox.Show("[Exception error: {0}]", 
       e.ToString()); 
     } 



    } // end change settings 
       public void onChange(object source, FileSystemEventArgs e) 
       { 
        ConfigurationManager.RefreshSection("appSettings"); 
       } 
       public void start() 
       { 
        FileSystemWatcher fileWatcher = new FileSystemWatcher(); 

        if (fileWatcher == null) 
        { 
         string path = Path.GetDirectoryName(config.FilePath); 
         string filename = Path.GetFileName(config.FilePath); 

         fileWatcher = new FileSystemWatcher(); 
         fileWatcher.Path = path; 
         fileWatcher.Filter = filename; 
         fileWatcher.NotifyFilter = (NotifyFilters.CreationTime | NotifyFilters.FileName); 
         fileWatcher.Changed += onChange; 
         fileWatcher.EnableRaisingEvents = true; 

        } // endif 


       } 

     } 

    } 
+0

把你的配置設置移動到單獨的文件/不同的存儲是一件大事嗎? – walther

+0

即時思考有一個文本文件單獨配置,讀取和寫入的URL沒有緩存問題。 –

回答

2

只要打電話ConfigurationManager.RefreshSection("appSettings");你叫appConfig.AppSettings.Settings["myConfigData"].Value;將強制應用程序讀取新&改變設置之前。否則,ConfigurationManager固有地緩存所有值。

+0

我在調用appConfig.AppSettings.Settings [「myConfigData」]之前調用了刷新。 –

相關問題