Reputation: 16555
Is there a way to write in a app.config file the information about endpoints?
Actually I want to read the <service>
tag from one app.config file and want to write it in other app.config file's <Services>
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.
Upvotes: 1
Views: 4502
Reputation: 754538
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:
Highly recommended! Jon Rista did a great job explaining the configuration system in .NET 2.0.
Upvotes: 3
Reputation: 1922
Take a look at the ConfigurationManager class
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?
Upvotes: 2