2016-03-21 7 views
1

Wir haben eine Anwendung, die eine Datei exe.config viel wie dieses hat:Wie aktualisiere ich die .NET-Konfiguration in einem benutzerdefinierten Konfigurationsabschnitt?

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 

    <configSections> 
     <sectionGroup name="ConsoleSettings"> 
     <section name="Console" type="MyApp.ConfigSection" allowLocation="true" allowDefinition="Everywhere" /> 
     </sectionGroup>  
    </configSections> 

    <ConsoleSettings> 
     <Console Username="User" Password="user" LanAddress="192.168.42.11" Port="8792" /> 
    </ConsoleSettings> 
.... 

Was ich die Datei lesen tun möchte ist, ändern Sie die LanAddress etwas der Benutzer (etwa string newLanAddress) eingegeben und dann Speichere es zurück.

Bisher habe ich dies:

var configFile = new ExeConfigurationFileMap(); 
var configFile.ExeConfigFilename = "MyApp.exe.config"; 
var config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None); 

var configGroup = config.SectionGroups[@"ConsoleSettings"]; 
var consoleSection = configGroup.Sections[0]; 
var lanAddress = consoleSection.// this is where I get stuck 

Wie greife ich auf das LanAddress Element consoleSection ??

+0

Können Sie nicht bekommen Sie den Wert mit 'System.Configuration.ConfigurationManager.AppSettings [key];' – Jacobr365

Antwort

1

Wir können benutzerdefinierte Konfigurationsabschnitt Klasse erstellen.

public class ConsoleSection : ConfigurationSection 
{ 
    [ConfigurationProperty("Username", IsRequired = true)] 
    public string Username 
    { 
     get 
     { 
      return (string)this["Username"]; 
     } 
     set 
     { 
      this["Username"] = value; 
     } 
    } 

    [ConfigurationProperty("Password", IsRequired = true)] 
    public String Password 
    { 
     get 
     { 
      return (String)this["Password"]; 
     } 
     set 
     { 
      this["Password"] = value; 
     } 
    } 

    [ConfigurationProperty("LanAddress", IsRequired = true)] 
    public string LanAddress 
    { 
     get 
     { 
      return (string)this["LanAddress"]; 
     } 
     set 
     { 
      this["LanAddress"] = value; 
     } 
    } 

    [ConfigurationProperty("Port", IsRequired = false)] 
    [IntegerValidator(ExcludeRange = false, MaxValue = short.MaxValue, MinValue = short.MinValue)] 
    public int Port 
    { 
     get 
     { 
      return (int)this["Port"]; 
     } 
     set 
     { 
      this["Port"] = value; 
     } 
    } 
} 

Um den Konfigurationsabschnitt zu lesen, sollten wir folgendes tun.

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
var consoleSection = (ConsoleSection)config.GetSection("ConsoleSettings/Console"); 
System.Console.WriteLine("ip: {0}", consoleSection.LanAddress); 

App.config ist Ihrem einen sehr ähnlich.

<configSections> 
 
    <sectionGroup name="ConsoleSettings"> 
 
    <section name="Console" type="MyApp.ConsoleSection, MyApp" allowLocation="true" allowDefinition="Everywhere" /> 
 
    </sectionGroup> 
 
</configSections> 
 
<ConsoleSettings> 
 
    <Console Username="User" Password="user" LanAddress="192.168.42.11" Port="8792" /> 
 
</ConsoleSettings>

0

Dies öffnet die Standardkonfigurationsdatei der Anwendung. Es ändert einen Verbindungsstringabschnitt, aber Sie sollten ihn ändern können, um Ihren benutzerdefinierten Abschnitt zu aktualisieren.

// get the config file for this application 
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 

// set the new values 
config.ConnectionStrings.ConnectionStrings["Connection Name"].ConnectionString = "Connection String Value"; 

// save and refresh the config file 
config.Save(ConfigurationSaveMode.Minimal); 
ConfigurationManager.RefreshSection("connectionStrings"); 
Verwandte Themen