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