tags:

views:

1326

answers:

2

Hi All, Is there a way to write in a app.config file the information about endpoints? Actually I want to read tag from one app.config file and want to write it in other app.config file's tag. Please anyone tell me how can I do it?

Actually I have a config file called "WCFService.exe.config" which i want to read in my program , so what i am writing is:

    string path = Path.Combine(Application.StartupPath, "WCFService.exe.config");
    Configuration config = ConfigurationManager.OpenExeConfiguration(path)  

                 ServicesSection serviceSection = 
 ConfigurationManager.GetSection("system.serviceModel/services") as ServicesSection;

                ServiceElementCollection sereleColl = serviceSection.Services;

but i get nothing in sereleColl , please have a look on this .

A: 

Take a look at the ConfigurationManager class http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager_members.aspx

I don't remember how to do it though.

Edit:

To access it you have to add a reference to System.Configuration.

Edit 2:

Changing the appsettings can be done like this:

        Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
        AppSettingsSection appSettings = config.AppSettings;
        KeyValueConfigurationElement setting = appSettings.Settings["MyAppSettingsKey"];
        setting.Value = "newValue";
        config.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");

You can access the WCF settings by typing:

ConfigurationSectionGroup sct = config.SectionGroups["system.serviceModel"];

Hope this helps.

Comment:

Your code is working fine here. However, I changed

string path = Path.Combine(Application.StartupPath, "WCFService.exe.config");

to

string path = Application.ExecutablePath;

This will use the config file of the application currently running. I don't know why your path doesn't work. Either it's that, or there must be an error in your config file?

Ezombort
actually i don't want to read the app.config of current executable but I want to read another app.config file which is "WCFService.exe.config".
Praveen
I am still able to load other configs using your method.
Ezombort
My only thought is that there must be something wrong in your config file, but I guess you have reviewed it :-)
Ezombort
marc_s is correct, even though we specify a path, it is the executables config that is loaded.
Ezombort
you mean i can read any config file , i just need to give the correct path.
Praveen
I'm not sure how it works, but you should read the articles posted by marc_s.
Ezombort
+1  A: 

Your calling "OpenExeConfiguration" - this will open the config for the currently executing app.

You need to read up on the .NET 2.0 configuration system: there's an excellent 3-part series on the .NET configuration system on CodeProject:

There's a series of really good articles on you to demystify the .NET 2.0 configuration system on CodeProject:

1) Unraveling the mysteries of .NET 2.0 configuration

2) Decoding the mysteries of .NET 2.0 configuration

3) Cracking the mysteries of .NET 2.0 configuration

Highly recommended! Jon Rista did a great job explaining the configuration system in .NET 2.0.

Cheers, Marc

marc_s