Adding and reading from a Config file
Solution 1:
Add an
Application Configuration File
item to your project (Right -Click Project > Add item). This will create a file calledapp.config
in your project.Edit the file by adding entries like
<add key="keyname" value="someValue" />
within the<appSettings>
tag.Add a reference to the
System.Configuration
dll, and reference the items in the config using code likeConfigurationManager.AppSettings["keyname"]
.
Solution 2:
Configuration configManager = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection confCollection = configManager.AppSettings.Settings;
confCollection["YourKey"].Value = "YourNewKey";
configManager.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(configManager.AppSettings.SectionInformation.Name);
Solution 3:
Right click on the project file -> Add -> New Item -> Application Configuration File. This will add an
app.config
(orweb.config
) file to your project.The
ConfigurationManager
class would be a good start. You can use it to read different configuration values from the configuration file.
I suggest you start reading the MSDN document about Configuration Files.