I have created a C# console based project
. In that project i have some variables like companyName
, companyType
which are Strings.
companyName="someCompanyName";
companyType="someCompanyType";
I need to create a config file and read values from it, and then initialize the variables companyName
, companyType
in the code.
- How can i create a config file (or equivalent) ?
- How can i read from the config file ?
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);
Right click on the project file -> Add -> New Item -> Application Configuration File. This will add an app.config
(or web.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.