.NET ConfigurationManager app.config confusion - c#

I've got an app.config file, which contains the following
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name ="PowershellSnapIns" type ="System.Configuration.DictionarySectionHandler,System"/>
</configSections>
<PowershellSnapIns>
<add key="SnapIn1" value="WebAdministration" />
<add key="SnapIn2" value="Jimmy Hendrix" />
<add key="SnapIn3" value="..." />
</PowershellSnapIns>
</configuration>
I was going to use the ConfigurationSettings class to read it, but that has been deprecated. That was fairly simple to use. Now I have to use the ConfigurationManager class, and I now have this code to read it.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
IDictionary SnapInList = (IDictionary) config.GetSection("PowershellSnapIns");
But it keeps erroring out. I changed the app.config properties to copy across to the build, but it keeps accepting that it can't find the file. The exception says it's looking for
TestConsole.vshost.exe.config. Does vs2k8sp1 now rename the app.config for you automatically, and if so, what am I doing wrong? Surely I don't need to rename the app.config file to debug vhost. I do know in release that it's probably being renamed TestConsole.exe.config. So what's going wrong? Is it the case of the code wrong or what?

Here's how I did it:
-Make sure that System and System.Configuration.dll are available in your bin directory. You do this by adding the reference to both dlls and set their copy local attribute to true.
-Make sure in your App.Config file, set the copy local attributes to false.
-Use the following method:
public static string XMLCheck
{
get
{
var section =(Hashtable)ConfigurationManager.GetSection("PowershellSnapIns");
return (string)section["SnapIn1"];
}
}
-The XMLCheck should return "WebAdministration"

VS renames the app.config to yourappname.exe and places it in the bin directory along with your .exe
Can you confirm that the file exists in the bin directory along with your exe.

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 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

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

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.

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"];

how to use with app.config

this is my app config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="version" value="1.0.0."/>
</appSettings>
</configuration>
this is my code
txtVersion.Text = ConfigurationSettings.AppSettings["version"];
I get null value
Try using the ConfigurationManager class:
txtVersion.Text = ConfigurationManager.AppSettings["version"];
You can use C# Settings instead.
http://msdn.microsoft.com/en-us/library/aa730869(VS.80).aspx
There is nothing wrong with your code, although ConfigurationManager now replaces ConfigurationSettings.
If you are running in Visual Studio, make sure your app.config has been copied to the bin\debug (or bin\release depending on if you are in release or debug mode) and has been correctly renamed to [yourappname].exe.config.
Make sure you have added the app.config to your project within Visual Studio (rather than just creating the file in the dir). Alternatively, put the file manually in the bin\debug dir with the correct name as above and try again.

Categories

Resources