Config-Datei für c#, windows-service

Habe ich diese Konfiguration Datei:

   <?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings>
        <add key="AuxAppStg0" value="5 iunie 2013 19:22:49" />
        <add key="AuxAppStg1" value="5 iunie 2013 00:00:00" />
        <add key="AppStg2" value="5 iunie 2013 19:23:04" />
    </appSettings>
</configuration>

Und ich will analysieren, es mit diesem code:

 //Get the configuration file.
        System.Configuration.Configuration config =
            ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        //Get the appSettings section.
        System.Configuration.AppSettingsSection appSettings =
            (System.Configuration.AppSettingsSection)config.GetSection("appSettings");



        foreach (KeyValueConfigurationElement i in appSettings.Settings)
        {

            Console.WriteLine("Key: {0} Value: {1}", i.Key, i.Value);
        }

        if (appSettings.Settings.Count != 0)
        {
            foreach (string key in appSettings.Settings.AllKeys)
            {
                string value = appSettings.Settings[key].Value;
                Console.WriteLine("Key: {0} Value: {1}", key, value);
            }
        }
        else
        {
            Console.WriteLine("The appSettings section is empty. Write first.");
        }

und alles was ich bekomme ist : Die appSettings-Abschnitt ist leer. Schreiben Sie den ersten.
Ich fand es hier. Was mache ich falsch? Ich möchte eine config-Datei für eine c# - windows-Dienst zu Lesen beim Start, ist das ein guter Ansatz, andere besser aproaches?

Was ist der name Ihre .config-Datei? Es sollte der name des service executable + .config am Ende, so MyWindowsService.exe sollte MyWindowsService.exe.config. Es sollte auch in dem gleichen Verzeichnis wie Ihre service executable. Sie auch brauchen, um starten Sie den windows-Dienst für Sie zu Holen, die änderungen an der Datei.
Warum bist du nicht mit den ConfigurationManager.AppSettings?
Gut fangen, +1 dafür.
wissen Sie, wie Sie sagen.. @matt-dot-net: true story!

InformationsquelleAutor AlexandruC | 2013-06-05

Schreibe einen Kommentar