Last post May 27, 2010 02:34 PM by mnotpit
Oct 09, 2007 09:18 PM|togakangaroo|LINK
Oct 09, 2007 09:29 PM|jguadagno|LINK
In Visual Studio, Choose the Solution Explorer, right click on the project for which you want to add a configuration file. In the Add New Item dialog, choose "Application Configuration File" and enter a file name.
You can use this new file to store application settings. Like
<add key="key1" value="setting1" />
<add key="key2" value="setting2" />
<add key="key3" value="setting3" />
<add key="key4" value="setting4" />
Within your code you can access them like so,
string mySetting1 = ConfigurationSettings.AppSettings["key1"];
string mySetting2 = ConfigurationSettings.AppSettings["key2"];
string mySetting3 = ConfigurationSettings.AppSettings["key3"];
string mySetting4 = ConfigurationSettings.AppSettings["key4"];
Oct 09, 2007 09:36 PM|togakangaroo|LINK
Oct 09, 2007 10:19 PM|jguadagno|LINK
If this was what you needed, please mark the post as an answer so that others can benefit.
Oct 09, 2007 10:45 PM|togakangaroo|LINK
Actually, it would seem that this did not fully solve my problem since I cannot access the ConfigurationSettings variable perhaps I need to include a reference or a using directive?
Oct 09, 2007 11:13 PM|jguadagno|LINK
Sorry add this to the top of the class.
Oct 10, 2007 12:22 AM|rampabbaraju|LINK
If you are using .NET 2.0 and Visual Studio IDE, you would want to use settings file. You can set type to each of the values you add. Visual studio automatically adds these values to the app.config and creates a rapper around your settings, so that you
can access them like application properties. (Ex: Properties.MyAppSettings.DevConnectionString)
Oct 10, 2007 01:53 PM|togakangaroo|LINK
Thanks, but I'm using vs2003, .net 1.1.
Im sorry to keep bringing everyone back to this but I followed the insturctions above and now I have access to ConfigurationSettings.AppSettings but it is constantly comping up empty! here's a snippet from my config file
-?xml version="1.0" encoding="utf-8" ?>
-add key="myLocation" value="test"/>
-add key="num_backups" value="2" />
-add key="temp_dir value="C:\\Temp\\" />
yet when I call ConfigurationSettings.AppSettings["myLocation"] I get null. Any ideas? I HAVE renamed my config file from the default app.config to myApp.config but I did it in the initial dialog box that came up to "add a configuration file". Can someone
tell me where the file is referenced in the code though as I would like to check that?
(Obviously the tags above are brackted, I just couldn't figure out how to place xml code on the page so I replaced the open bracket with a -)
Oct 10, 2007 04:36 PM|togakangaroo|LINK
Apr 19, 2008 07:58 PM|mrex|LINK
Just what I was looking for!
May 27, 2010 02:34 PM|mnotpit|LINK
Thanks for the info! I just had a minor follow-up clarification: the ConfigurationSettings
class is now deprecated/obsolete. Instead, use the System.Configuration.ConfigurationManager class. It provides a similar AppSettings property that works the same way. To access the ConfigurationManager class, you may need to add a reference to the
System.Configuration .NET assembly.