I can't load my configuration file in my C# Application - c#

I'm using a config file in my Library project in order to associate the interfaces with their own classes; I'm having troubles since my Application can't load anything from the config. Here is a sample from the config file,which is called app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Sic2Lib.it.carrefour.sic.profiler.datasource.def.AuthenticateDS" value="Sic2Lib.it.carrefour.sic.profiler.datasource.impl.AuthenticateDSImpl"/>
<add key="Sic2Lib.it.carrefour.sic.profiler.datasource.def.CheckUserDS" value="Sic2Lib.it.carrefour.sic.profiler.datasource.impl.CheckUserDSImpl"/>
<add key="Sic2Lib.it.carrefour.sic.profiler.datasource.def.ReadApplicationConfigDS" value="Sic2Lib.it.carrefour.sic.profiler.datasource.impl.ReadApplicationConfigDSImpl"/>
Which is held in the same directory of the DataSourceFactory Class. This class is supposed to take the setting through the command
NameValueCollection keys = ConfigurationManager.AppSettings;
So, I build the project with no errors and I get a file called myProject.dll.confing in the bin/Debug folder. But after all this I always get the keys variable empty...How come? What's wrong with what I've done so far?

Library projects do not have their own configuration - they use the configuration from the application that uses the library.
Put the configuration settings in the configuration file of the application project and you should be fine.

Related

reading app settings as null from App.config

I am struggeling reading data from a configuration file, and all the methods online are not working for me...
I have this configuration file (App.config):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="test" value="testVal"/>
</appSettings>
</configuration>
And this line in my C# code
string appSettings = ConfigurationManager.AppSettings["test"];
for some reason, appSettings remains null when expected to be testVal.
This should work. Put a break point right after that and check the value. I tested same code and getting the value.
If you use web application make sure you put this in web.config and not in the app.config
Something that may be happening is the App.config isn't being included in the build output. Check the bin folder of your project directory (Debug/Release), and ensure there's a file that matches the namespace of your project with a ".config" extension. You should see, for example:
YourApp.dll
YourApp.exe
YourApp.dll.config (edit this in notepad to verify it contains the
appsetting values)
...
Assuming this is the case and you don't see the config, or it doesn't have the correct values, ensure the App.config file in your project is included in the project itself (not just added manually to the project directory through file explorer).
If it is included and still not working, try deleting and re-creating a new "Application Configuration File" in your project in Visual Studio... that should clear things up (hopefully!). Be sure to save the original app.config contents beforehand when doing this.

How can I use several Application Configuration Files in one project?

After creating new Visual C# Console Application (.NET Framework 4.5), such project contains default App.config file.
After adding a reference for System.Configuration to project and use it in some source file using System.Configuration; I can use static class ConfigurationManager to operate with App.config file. But before, I want to add some settings to the file, so it's somehow like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DeployWeb" value="true" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
Now, I can write something like this, to get the value of the setting:
Boolean deployWeb = false;
Boolean.TryParse(ConfigurationManager.AppSettings["DeployWeb"], out deployWeb);
However I didn't set which configuration file to read,but it's okay, because there is the default one. But I can add more configuration files by right click on the project -> Add -> New Item... -> Application Configuration File, so that I have, for example, 5 configuration files, like on the picture:
And ConfigurationManager will still read the default one, but I want to manually control, which config file to use. I want to know, if is there an appropriate way to set to the ConfigurationManager config file name to use etc. and if it's not a bad practice. I know how to use different configurations in debug/release mode, but in my case I have an application, that can be runned in different modes for different purposes in release, for example.
Question: Is it possible to have several configuration files in a project and to have ability to switch which one I want to use. Isn't it a bad practice, shall I use some another approach for my purpose ? Using build events is not suitable in my case (I think).
PS. I am sorry for stretching my question, however my itis quite simple and could be just asked in two sentences, but as the rules says, question should contains details.
UPDATE:
From already existing answer "Option: You can use the ConfigurationManager Class to load an alternate config file by code." From reading msdn I didn't get which of the methods should I use. Should I open Exe configuration ?
It can be done using mapped config file ,
ExeConfigurationFileMap configFileMap =
new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = "App4.config"; // full path to the config file
// Get the mapped configuration file
Configuration config =
ConfigurationManager.OpenMappedExeConfiguration(
configFileMap, ConfigurationUserLevel.None);
//now on use config object
AppSettingsSection section = (AppSettingsSection)config.GetSection("appSettings");

How to use the value of appSettings from App.Config file when creating a Windows Service

I am trying to create a Windows Server. I have some logic in C#
string urlToPing = ConfigurationSettings.AppSettings["UrlToPing"].ToString();
Stream data = client.OpenRead(urlToPing);
I need to read
Here my App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="UrlToPing" value="http://mysite.com"/>
</appSettings>
</configuration>
I am new at Windows Services, my questions:
When I publish to folder the Service or if I create a build I cannot
see the App.Config file
Visual Studio warning on ConfigurationSettings.AppSettings as obsolete (what should I use instead?)
To my second question I found a solution:
Add a reference to System.Configuration to your code file.
using System.Configuration;
The setting may now be referenced correctly...
ConfigurationManager.AppSettings["UrlToPing"].ToString();
To your first question, when you build a executable project (Windows Service, Console Application, etc) it will rename the app.config to "YourApplication".exe.config where "YourApplication" is the name of your Startup assembly. It will then copy the file to your output folder.
Add System.Configuration to references

How to read values from multiple Configuration file in c# within a single project?

Here in my project I have two application configuration files called app.config and accessLevel.config. Now using the OpenExeConfiguration I was able to access the app.config.exe file but not the accessLevel.config. Please help on this.
The main reason I have 2 config files is to show the difference and make the code simple.
I need to read the values from the accessLevel.config in my C# code.
Tried the below code but no use:
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.File = "App2.config";
See here.
Put this in your App.config:
<appSettings file="accessLevel.config"/>
And then have another file called accessLevel.config like this:
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="TestSetting" value="TestValue"/>
</appSettings>
And then you can access your config values in code like this:
string value = ConfigurationManager.AppSettings["TestSetting"];
Make sure that accessLevel.config is set to copy to the output directory (right click the file in Visual Studio -> Properties -> Copy To Output Directory -> Copy if Newer).

Configuration from App.config isn't being pulled correctly

I'm trying to extract a URL I saved to the app.config file, but it's returning a blank string. Any ideas why?
string asdf = String.Format("{0}", ConfigurationManager.AppSettings["MemberUrl"]);
And the configuration file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ForumUrl" value="http://www.dreamincode.net/forums/xml.php?showforum=" />
<add key="MemberUrl" value="http://www.dreamincode.net/forums/xml.php?showuser=" />
</appSettings>
</configuration>
If the app.config is part of a class library it probably isn't being copied to the bin folder properly (if at all).
The config file must be named <exefilename>.config for it to be picked up by the running application.
The App.config file in the application project (the one that produces an exe file, Console, WinForms, etc.) will copy and rename on deployment. Or if this is being executed from a web project it needs to go in the web.config.
Does this help?
All config information that your class library needs must be in the main projects App.config or web.config. In other words, if your app.config file is attached to the library it will NOT be read.
Go to the main application and add the appropriate keys/values to it's config file.
Sergio I just tried this is a console application and it works perfectly.
I would suggest that it's a class library; and not a main assembly that you have added your app.config file to.
When you do a build; look in the binary output folder Debug or Release and in there you should see a file named yourEXEfilename.config; if that file is not there then you will not get any output from the line of code you have above.
AppSettings will return a NULL string.
Hope this is of use
Kind Regards
Noel
there's no reason why that wouldn't work - do you have any other pertinent info ?
FYI, you dont need String.Format for what you're doing, the following is fine
string asdf = ConfigurationManager.AppSettings["MemberUrl"];

Categories

Resources