How to access external .config files? - c#

I have an asp.net mvc website which of course has a web.config file. I also have an external project which is a class library that uses a .config file for its own app settings. The problem is when I run my web application those external app settings values are not included in the appSettings.
How can I get the external class library projects appSettings values?

You can get the external app setting like that :
var config = ConfigurationManager.OpenExeConfiguration("some.config");
var someKeyValue = config.AppSettings.Settings["someKey"].Value;

You need to either:
1. add those settings to your web.config file.
2. point to the external settings, and use a post build event handler to copy the output into your web project.
<configuration>
<appSettings configSource="my.config" />
</configuration>
Standard convention is that you add the settings to your web.config file. assemblies that are only dlls do not load their own config files. This allows people who use them to specify the settings in their own application.

Related

Is Web.Config the same as app.exe.config? [duplicate]

when building a desktop app in wpf can you read documentation of problems and safely subsititute 'app.config' when people's answer's refer to 'web.config'?
if so are there any glaring GOTCHAS you have to look out for?
tnx
Read the Documentation:
Web.config and App.config
The choice of the
configuration file name is determined by the hosting environment you
choose for the service. If you are using IIS to host your service, use
a Web.config file. If you are using any other hosting environment, use
an App.config file.
In Visual Studio, the file named App.config is used to create the
final configuration file. The final name actually used for the
configuration depends on the assembly name. For example, an assembly
named "Cohowinery.exe" has a final configuration file name of
"Cohowinery.exe.config". However, you only need to modify the
App.config file. Changes made to that file are automatically made to
the final application configuration file at compile time.
In using an App.config, file the configuration system merges the
App.config file with content of the Machine.config file when the
application starts and the configuration is applied. This mechanism
allows machine-wide settings to be defined in the Machine.config file.
The App.config file can be used to override the settings of the
Machine.config file; you can also lock in the settings in
Machine.config file so that they get used. In the Web.config case, the
configuration system merges the Web.config files in all directories
leading up to the application directory into the configuration that
gets applied.
Web.Config is used for asp.net web projects / web services.
App.Config is used for Windows Forms, Windows Services, Console Apps and WPF applications
Your question isn't providing all the information as to where the gotcha's may lie for you.
Can you give us more info on what you are trying to do in terms of these config files?
Here's a link...
Problems with Web.config and App.config

Copy settings from one Web.config to another except AppSettings and ConnectionStrings

Is it possible, through code, to copy settings from one Web.config file to another (except AppSettings and ConnectionStrings)?
I have a situation where a single MVC3 project has been deployed to multiple servers in different locations. There is an auto-updater on all of these that will pull in the latest version. Typically when this would run, it would only overwrite the application folders and NOT the Web.config file.
I just upgraded the project to MVC4. This changes basically everything in the Web.config file except the AppSettings and ConnectionStrings. All of the installations of this project would have slightly different values here.
How would I go about writing some code that will update the Web.config file, but preserve all of the AppSettings and ConnectionStrings?
.NET provides ways to get configuration from other config file. Refer this article:http://blog.andreloker.de/post/2008/06/Keep-your-config-clean-with-external-config-files.aspx
Basically, you can use "configSource" attribute to define which config file to refer. Note that the configuration file should be in same directory. if not, Refer here to solve the problem. .NET Config Files configSource outside the application directory folder

How to access Configuration file in Class library from webpage as well as webform

I have several projects some of which are web applications and some are windows applications.
I wanted to globalize my settings, something like Connection Strings because all the projects use the same connection string.
So I followed this example How to share custom application configuration settings across projects in .NET
my XML file is in the class library and that is referenced in each project. The windows project are working fine with the file path which is something like this in the client project:
<configuration>
<appSettings file="F:\Classes\ConnectionStringFile.xml">
</appSettings>
</configuration>
I also want to be able to use this xml file from a web project. however, the web project would not accept the path to this file as shown earlier.
What is the best approach or say, best solution to have this connection string file shared amongst web projects as well as windows projects. What do i pass in
something like <appSettings file ="~/classLibrary/ConnectionStringFile.xml" ???`
The solution we use is to store shared settings in a custom folder beneath the CommonApplicationData folder (e.g. C:\ProgramData on Windows7):
var commonAppData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
var settingsFilePath = Path.Combine(commonAppData, "MyProgram\\Settings.xml");
file= accepts relative paths, and *.config files are excluded from ASP.NET and IIS static file rules, so it is safe to drop any *.config file into an ASP.NET site. Just put it next to the web.config file (root of the web app), mylibrary.config, and file="mylibrary.config".

App.config problem

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.

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