Read config file with OpenExeConfiguration? [modified]
-
Hello. Right now I admit defeat and don't know how to do this. I have a configuration file that I have to load at runtime and interpret. This works fine for all the settings in the appSettings sections but I don't know how to get the value from the system.diagnostics section. This is an example of an config file (in "real" a lot bigger;))
<configuration>
<appSettings>
<add key="Hello" value="World" />
</appSettings><system.diagnostics>
<switches>
<add name="Trace" value="4" />
</switches>
</system.diagnostics></configuration>
To load the file I use the OpenExeConfiguration method and most of the data I can get to, but not the "Trace" value from the system.diagnostics.
// Get the configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration("c:\\projext1.exe"); MessageBox.Show(config.AppSettings.Settings["Hello"].Value); //this shows OK ConfigurationSection sel = config.Sections["system.diagnostics"]; // Here I find the section, but then... ???
Does anybody have any idea on how to get the value? A possible solution might be to load the file into XML, but I'm still hoping on using the Configuration object. Hope you can stand my bad english and thanks to enybody that have a suggestion. Edit: formatting... :sigh:
-
Hello. Right now I admit defeat and don't know how to do this. I have a configuration file that I have to load at runtime and interpret. This works fine for all the settings in the appSettings sections but I don't know how to get the value from the system.diagnostics section. This is an example of an config file (in "real" a lot bigger;))
<configuration>
<appSettings>
<add key="Hello" value="World" />
</appSettings><system.diagnostics>
<switches>
<add name="Trace" value="4" />
</switches>
</system.diagnostics></configuration>
To load the file I use the OpenExeConfiguration method and most of the data I can get to, but not the "Trace" value from the system.diagnostics.
// Get the configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration("c:\\projext1.exe"); MessageBox.Show(config.AppSettings.Settings["Hello"].Value); //this shows OK ConfigurationSection sel = config.Sections["system.diagnostics"]; // Here I find the section, but then... ???
Does anybody have any idea on how to get the value? A possible solution might be to load the file into XML, but I'm still hoping on using the Configuration object. Hope you can stand my bad english and thanks to enybody that have a suggestion. Edit: formatting... :sigh:
Be sure that you are:
using System.Diagnostics
Then, in your code, you can use the configured switch by creating a TraceSwitch with the same name as in your config file:TraceSwitch appSwitch = new TraceSwitch("Trace", "");
The value you want to retrieve is appSwitch.Level (which in your case will beVerbose
, as you have the value set to 4 in your **config file.SkyWalker
**