2012-06-08 141 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”]之前调用了刷新。 –

相关问题