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.
Related
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).
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"];
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.
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.
I've developed a windows service application using Visual Studio 2008 / C#.
I have an app.config file in the project. When installed, the app.exe.config file appears beside the executable but it appears not to be reading the values from it when I try to access them through ConfigurationManager.AppSettings.
Has it copied the config file elsewhere or is there some other problem I don't know about?
Thanks in advance,
Martin.
Edit:
The config file name is infact my_exe_file_name.exe.config, it looks like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="RuntimeFrequency" value="3" />
</appSettings>
</configuration>
and I am trying to read via:
ConfigurationManager.AppSettings["RuntimeFrequency"]
The debug value I continually see is '1' and not '3'. Am I doing something wrong here?
I located the error and it was related to file permissions. After installing the service, my local user account didn't have access to modify the app.exe.config file.
The tool I was using to edit was not informing me it was being denied access to save the file - that's notepad++ if anyone is interested - so I couldn't see that it wasn't saving over the old config file.
Solved now, thanks everyone.
Martin.
When you are in debug mode check and see what settings are in the my_exe_file_name.vshost.exe.config Also make sure you adjust this in the app.config file. Visual studio should update the final config file in your bin/debug folders.
Maybe you are updating the wrong config file. You should double check that using
System.Configuration.ConfigurationManager.OpenExeConfiguration(PATH_TO_CONFIG);
Generally for the Windows Services that I write, i drop the appName.exe.config file into C:\WINDOWS\system32\
Perhaps you have an older version in that directory, which is where your service is getting the value, even though you've updated the config file in your project.
App.config file should be renamed to your_exe_file_name.exe.config and placed near the exe file.
Is it possible that you have more than one instance of the RuntimeFrequency entry defined? The ConfigurationManager reads the file from top to bottom and processes each setting individually. Therefore, the last value of RuntimeFrequency that is defined in the file is the one it will use.
If you want to know for sure if your file is being used, I would simply remove or comment out any definition for RuntimeFrequency (copy/paste errors do happen) and wait to see an application error when ConfigurationManager attempts to reference an entry in AppSettings that does not exist.