App.config problem - c#

i am having a classlibrary with app1.config and also a windows application with app2.config;i am adding the reference of classlibrary in windows application as well as app1.config.is it possible if i call the method class lib it will go to app1.config otherwise it will use app2.config;

The best you can achieve is to have two separate configuration files, then have the code of one method read the "main" config file (using the ordinary ConfigurationManager.AppSetting[""] code) and other method read the configuration file of the class library using such code:
Configuration config = ConfigurationManager.OpenExeConfiguration(dllFilePath);
KeyValueConfigurationElement element = config.AppSettings.Settings[appSettingKey];
string value = element.Value;
This will read application setting from the config file of DLL sitting in the location of dllFilePath.
You can also have separate section for the class library in the "main" configuration file if relevant I can give sample as well.

Using the default ConfigurationManager process to access a configuration file it goes for the file that is configured for the application, there is no way to differentiate between a class library and an application's config.
For example if you have a windows/WPF app that is called MyWonderfulApp.exe, the only config file that will be used is MyWonderfulApp.exe.config. Therefore all settings are in that file. Web applications ONLY use the web.config file.

Related

How to set the app.config file for the current assembly in C#?

I have integrated a dll with a service(exe). The dll has its own app.config file. But the code inside the dll for any Key/Value reference, it always refers to the exe's config file instead of the dll's config file. How the make the dll's config file as current to be used only when referred from inside the dll's code?
A DLL (or library file) cannot have its own config file. It will ALWAYS use the "hosting" applications configuration files.
The whole point of a DLL is that is can be used by many different applications and does not have configuration itself. Instead its "host" will contain the configuration information, so this is "by design".
When your solution contains more than one project, you can add app.config in any of the project; but when you compile the code to generate final output, you get only one configuration file named <exeFileName>.exe.config . This is the only configuration file that will be read by the WinForms framework when the application is running.
If your DLLs (Assemblies in .NET) specify their own configuration file, those config files will not become a part of the final output (the respective assemblies).
To include the settings from the project specific config files, you will have to make those configuration entries (appSettings, connectionString, etc) into the main application's configuration file.
The DLLs can also read from the configuration file of the main application. Hence, when you make those entries in the <exeFileName>.exe.config file, all the DLLs, that are loaded can read values from the file.
Note: Take care of using unique names for the keys for each DLL as the configuration values are read using the keys.

Using one app.config file for multiple projects

I have a solution with the following setup:
X amount of class library projects
Y amount of console application projects
Each of these projects may have 0 or more configuration parameters.
Now, I'd like to have only one App.config for user to specify settings and that App.config will only contain parameters of all the reference projects of the console application project to be run.
I've tried giving each project a Settings file and then linking them to the console applications according to their dependencies but that didn't work.
I've also tried just lumping all the configurations together in one class library project and have each console application link to that app.config (or settings file). But that also didn't work (i.e. changes of the app.config or the settings file in the class library will not update the .config of the executable)
Is what I am trying to do possible?
Yes, it is possible. You just need to open the app file. Follow the next example:
ConfigurationManager.OpenExeConfiguration("C:\Test\SomeProject.dll");
XmlNode loggingConfigNode = ConfigurationManager.GetSection("log4net") as XmlNode;
I guess that you will have to open each setting file in order to use the settings, or you will have to consolidate all the settings in a single app.config and then your applications can access the file by open it.

Use ConfigurationManager with custom path to config file

I see many solutions to reading in values from an external configuration file in a C# console application, but I can't find a solution for my particular circumstances:
Due to reasons of deployment (mainly that this is for a console application that is packed for deployment as part of an MVC website using the Visual Studio web Publish method), the exe does not get packaged with its app.config file.
I'm dependent on libraries that make use of the ConfigurationManager.AppConfig["blah"] syntax, and I can't very well pass in my own AppSettingsSection to these libs.
My console application's .exe file is in the MVC app's bin directory. As both the website and the console app use the same config values, I was trying to simply load the site's Web.config file into the console app, but I haven't found a way of doing this.
The default configuration file is loaded once the AppDomain is loaded. For console applications, the configuration file must be located in the same place as the executable.
I believe that one possible solution is loading an AppDomain as a child of the console application and setting up the AppDomainSetup.ConfigurationFile property rightly to load the configuration file from the custom location, and then execute the whole console application's logic inside the child AppDomain.
You can use this AppDomain.CreateDomain overload for this case (the MSDN article contains a sample code on how to provide the AppDomainSetup):
http://msdn.microsoft.com/en-us/library/aehss7y0.aspx
As far as I know, you can't change the default behavior of where the executable AppDomain looks for the configuration file once it's loaded, but as I suggested you, you can create your own AppDomain with your own requirements!

Common app.config for multiple applications

I have several C# console applications, which need to have the same set of settings. I want to avoid duplicity and avoid separate app.config for each application.
Is there any way to read a common app.config file (say common.config) for applications (app1.exe, app2.exe).
Create one file called app.config. Put it in some place outside of your projects' directories, like up in the solution directory. Add it to your projects as a linked item with a relative path to the file. Set the right build action for this item (application configuration) in each project.
Now when each project builds, the file will be copied to the project's output dir with the right name.
You can load an external app.config using the code below:
config = ConfigurationManager.OpenExeConfiguration(Path.Combine("C:\test\root", "Master.exe"));
string logpath = config.AppSettings.Settings["Log.Path"].Value;
And save settings as so:
config = ConfigurationManager.OpenExeConfiguration(Path.Combine("C:\test\root", "Master.exe"));
config.AppSettings.Settings["Log.Path"].Value = "C:\newpath";
config.Save();
You might have to have a master config within one of the applications and point the rest to this. Typically this method is considered bad practice though. There might be issues with different applications locking the file.
#Ran's answer is an option, but each application will still have its own config file after you build. At compile time they will be the same, but at deploy time they are copies.
You can also open one application's config file from another application using:
ConfigurationManager.OpenExeConfiguration(string)
You can have an external config file that all applications reference using:
ConfigurationManager.OpenMappedExeConfiguration
And there's the option to using the Machine config file using:
ConfigurationManager.OpenMachineConfiguration()

C# Reading Configuration settings

I have a C# class library A which has some configuration settings in its App.config I access them with
Method1()
{
string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
}
But when I call Method 1() from my ASP Web project B, it cannot find the configurations settings in the Class library A
Any idea what is happening here?
The entire configuration management structure created by .Net runtime, is process-specific. not assembly specific. This means that each running executable gets an app.config. A Web project gets a web,config (actually a web project can have multiple web.configs), but assemblies cannot have their own app.configs, they can have code to read the configuration settings in the config file for whatever process they are being referenced in (which use the assembly as a reference in a winforms app, then it can see config settings in the MyWinformsApplication.exe.config; Use the assembly in an ASP.Net web app, then it can see confiog settings in the web applications' web.config...
The config settings have to be copied to your web.config. Essentially there is only one default config file per project which the ConfigurationManager reads.
A library doesn't have its own configuration file. Configuration settings should be defined in the exe that uses that library
I believe you can use OpenExeConfiguration to do this:
string exePath = "<full path and name of the app .exe file>";
System.Configuration.Configuration otherConfig =
ConfigurationManager.OpenExeConfiguration(exePath);
You could put the path of the other .exe in the web app's web.config (for instance, in the appSettings section), and read it from there, which would be better than hard-coding it here.
to view the appSettings in that config file:
AppSettingsSection otherAppSettings = otherConfig.AppSettings;
This MSDN page might help.
It's looking for the configuration setting in your web project.

Categories