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.
Since CONFIG files are stored in plain text, you can view and edit them with a text editor such as Microsoft Notepad.
The CONFIG File format is used for server processes, software applications, and operating system settings. A programmer can write code to instruct a software to read the configuration files again and again after certain time of period and apply the changes to the current process.
Add an Application Configuration File
item to your project (Right -Click Project > Add item). This will create a file called app.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 like ConfigurationManager.AppSettings["keyname"]
.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With