The application needs to use the settings from the configuration file of another application on the same machine. Can I use the classes from the System.Configuration namespace or I should parse it as xml-file?
You can read any configuration file using the ConfigurationManager.OpenMappedExeConfiguration API.
There is a complete working example in the linked documentation.
This try:
ConfigurationFileMap fileMap = new
ConfigurationFileMap(<filename>);
Configuration managerConfiguration =
System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
Related
My BizTalk application requires me to add a custom behaviorExtension to my machine.config file. I install my application via MSI, via BizTalk Deployment Framework (BTDF), so I would like this to be done programmatically as well.
Now I cannot seem to find a way to either list installed behaviors not edit them.
I have the following code, but after that I'm stuck.
// Get the machine.config file.
Configuration machineConfig = ConfigurationManager.OpenMachineConfiguration();
// Get the machine.config file path.
ConfigurationFileMap configFile = new ConfigurationFileMap(machineConfig.FilePath);
// Map the application configuration file to the machine
// configuration file.
Configuration config = ConfigurationManager.OpenMappedMachineConfiguration(configFile);
ConfigurationSectionGroup svcModel = config.SectionGroups.Get("system.serviceModel");
ConfigurationSection extensions = svcModel.Sections.Get("extensions");
Can anyone give me some pointers on how to approach this?
You are almost there. Your extensions variable is of type System.ServiceModel.Configuration.ExtensionsSection, which has property BehaviorExtensions containing what you are looking for. So:
var extensions = (System.ServiceModel.Configuration.ExtensionsSection) svcModel.Sections.Get("extensions");
var behaviors = extensions.BehaviorExtensions;
Is there a way (maybe from command line) to start the same exe file twice using a different app config?
var configMap = new ExeConfigurationFileMap();
configMap.ExeConfigFilename = #"myconfigpath";
var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
Start it from a separate location, where the filename.exe.config is different.
THe configuration framework let you load the config from any file but you will have to handle that manually from within your application.
Look at ConfigurationManager class
You can load your process into a separate app domain. This allows you to redirect the configuration file. See http://www.codeproject.com/KB/IP/HostingMultipleServices.aspx for an example of doing this with a Windows service. It'll be similar for any kind of application.
I have a Windows application called TechReader. Its configuration file is TechReader.exe.config. Some parts of the configuration are kept in other configuration files. So I refer to that config file from the config section created in the exe.config.
<TechReader.ProviderConfiguration file="localProvider.config"/>
Now I want to load the whole configuration of my application using reflection. I use code like this.
Assembly techReaderAssembly = Assembly.GetAssembly(typeof(TechReaderStarter));
ConfigurationManager.OpenExeConfiguration(techReaderAssembly .Location);
TechReaderStarter class is defined in the project whose output is windows application and not the library.
When I use above code, I get TargetInvocationException and ConfigurationErrorsException
Is the approach correct?
Will OpenExeConfiguration load the final configuration generated by merging the exe.config and other referenced config file?
How can I achieve the things?
Note: I want to use this Windows application to install as a Windows Service. I am trying to read the configuration in the ServiceInstaller class (a class which inherits ServiceInstaller) so that the details like service name mentioned in the configuration of the Service will be available to the installutil. For this I have to use reflection to get the exact configuration of the service.
I am creating one class library project.
Now by default I have one App.Config file so that I am putting all environment specific data in that Config file.
Now based on the Environment (whether Dev / Test / Production), I am planning to have three App.Config files in VS 2010 such as
App.Dev.Config
App.Test.Config
App.Prod.Config
Wondering how would the application know which config file to use.
Anyone implemented this scenario. Any Code samples / Articles would be helpful.
Thanks
The app will use the config file named YourExcecutable.exe.config which is by default the file App.config included in your (executable) project.
Note, that .NET only loads one config file for the whole application. You cannot use multiple configuration files (i.e. one per library project) without coding.
Option: You can use postbuild events and different solution configurations to copy one or another App.Config file to the output folder
Option: You can use the ConfigurationManager Class to load an alternate config file by code.
Loading a different application configuration file at run time can be done using the concept of mapped configuration file. To start with, you need to add reference to System.Configuration.dll in your project.
Set the value of Copy to Output Directory property to Copy if newer (Refer screenshot). This has to be done only for non-default configuration files e.g. App1.config, App2.config, etc. Leave the default configuration file namely App.config as it is . Due to this change, all the non-default application configuration files will be available in the project output directory (\bin\debug) when the project is built. Default value of this property is Do not copy.
Here is the code snippet on how to read configuration data from non-default configuration files:
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = "App1.config"; // app1.config should be present in root directory from where application exe is kicked off
// Get the mapped configuration file
var config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
//get the relevant section from the config object
AppSettingsSection section = (AppSettingsSection)config.GetSection("appSettings");
//get key value pair
var keyValueConfigElement = section.Settings["appSettingsKey"];
var appSettingsValue = keyValueConfigElement.Value;
If you have multiple application (aka app) configuration files then you can keep a setting in default App.config file with the help of which you can make a decision at run time about which non-default configuration file to load e.g. App1.config
Note: Now look at below code:
ConfigurationManager.AppSettings["DeployEnv"]
This code will still read the data from the default App.config file. This behavior can't be changed. There is no way to prohibit the Loading of default App.config file. You have to use alternate means as discussed in this post to read the data from non-default configuration files
Now there is an even better solution: SlowCheetah - XML Transforms
I have a .NET 3.5 class library I built that reads an App.config file for values it needs. It can pull the config values just fine when I test it in Visual Studio. To test it, I just change the project to a console application and execute a method call.
I have the need to call this class library from many other .NET programs, and I want the class library to be self sufficient (I should be able to call it from any other program, and it should use its own config file, not know about any calling config file etc.).
I can add a reference to the dll (since I am still development I am using VS 2008, haven't thrown anything into the GAC yet) but the App.config that the class library is reading is from the calling program's App.config, not the class library's App.config.
The class library dll has it's config file in the same directory, so it should be able to find it just fine, and the calling application is named differently. I am using the standard key value pairs in the App.config (e.g. name of config file myClassLibrary.dll.config) and getting values out with the following line of code:
String myVal = ConfigurationSettings.AppSettings["myConfigSetting"];
Does anyone know how to fix this?
An app domain in C# can have only one assembly level app.config file. See here on MSDN. An executable will always start up an AppDomain and by default look for a config file with name: EXECUTABLE_NAME.config. For example, SampleApp01.exe will look for SampleApp01.exe.config as its configuration file.
you can place your configs in the machine.config file inside the framework folder by this way you can globally use your configuration in all .Net applications running in that machine,
I believe app.config will always be used by the executable. Just drop it in that directory.
They would do that to ensure the dll can be shared and not have to share the same .config file.
You might be able to create a link from the executable .config file
<appSettings configSource="\lib\app.config">
Or change its name, i don't understand how you can have both app.config files in the same directory..don't they have the same name?
<appSettings configSource="\lib.app.config">
I can't find a way to avoid getting the app.config for the calling dll/exe etc. The only way I have found is to use a hardcoded path and load it that way. Here is code I am using to do that:
using System.Configuration;
...
public static KeyValueConfigurationCollection getAppSettingsFromAppConfig(String appConfigPath) {
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = appConfigPath;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
AppSettingsSection section = config.AppSettings;
KeyValueConfigurationCollection appsettings = section.Settings;
return appsettings;
}
You then have a collection of KeyValueConfigurationElement, which you can use .Value to get the string from config file with.