2010-11-23 11 views
3

dies ist mein Code ...C# Datei app.config

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal); 
    AppSettingsSection configSection = config.AppSettings; 

    try 
    { 
     if (configSection != null) 
     { 
      if (configSection.IsReadOnly() == false && configSection.SectionInformation.IsLocked == false) 
      { 
       configSection.Settings["DailyFilName"].Value = "NewValue"; 
       config.Save(); 
      } 
     } 
    } 
    catch (ConfigurationException ex) 
    { 
     MessageBox.Show(ex.Message, "Configuration Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 

Aber es ist nicht meine Config-Datei aktualisieren :(

+3

haben könnte sind keine Compiler, um Ihren Code zu lesen und zu verstehen, wie er kompiliert/ausgeführt wird. Etwas mehr Erklärung würde dir und uns helfen ;-) – Shoban

+0

Zeigt es irgendeinen Fehler? – Shoban

+0

Welchen Umfang haben die Konfigurationswerte? – SubniC

Antwort

1

sollten Sie dies:

configSection.SectionInformation.ForceSave = true; 
config.Save(ConfigurationSaveMode.Modified); 

einen Blick in der ConfigurationSaveMode Enumeration für weitere Optionen

+1

auch, gerade bemerkt, der Name der Einstellungen ist nicht korrekt "DailyFilName" geschrieben. vielleicht ein Tippfehler hier? –

+0

danke Daniel arbeitet. – Charitha

+0

großartig! Ich schätze eine Abstimmung, wenn es hilfreich war :) –

0

ersetzen config.Save();

config.Save(ConfigurationSaveMode.Modified); 
ConfigurationManager.RefreshSection("appSettings"); 
1

Hier ist eine Lösung aus dem Artikel zu How to change App.config file run time using C#

// Open App.Config of executable 

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

// Add an Application Setting. 

config.AppSettings.Settings.Remove("LastDateFeesChecked"); 

config.AppSettings.Settings.Add("LastDateFeesChecked",  
DateTime.Now.ToShortDateString()); 

// Save the configuration file. 

config.Save(ConfigurationSaveMode.Modified); 

// Force a reload of a changed section. 

ConfigurationManager.RefreshSection("appSettings"); 
+0

Verbatim nicht attributierte Kopie von http://chiragrdarji.wordpress.com/2008/09/25/how-to-change-appconfig-file -run-time-using-c / –

0

Ich denke, ist das Problem, das Sie Ihre Konfigurationsparameter als "user-scoped" Wir haben nicht