When ever .net application runs in any computer, it requires storing some user preference data, connection string if it’s interacting with database and setting for user’s custom control over application. To do so, user adds custom class that stores all information and retrieve when need arise. Usually system administrator wishes to change setting information.
If information is stored in XML file, its easy to store, edit and retrieve by application, with help of System.Configuration namespace that task can be fulfilled.
There are two types of configuration files exist. Machine.Config, it is one which stores information for all application running in a computer, that defines setting for machine. Application .cofing files which stores information for a particular application and which resides in executable folder of running assembly.
Some of the settings which are exists in Machine.Config can be overridden in application configuration but only whose setting allowDefination property set to MachineToApplication , whose property set to MachineOnly can’t be override in application configuration.
To play with setting files, System.Configuration require, we can add reference to it by selecting add reference to our project and once its added to our application, it can be used by Imports in vb.net and using in C#.
To edit Machine.Configue file following steps are useful.
Create instance of Configuration by calling ConfigurationMangaer.OpenMachineConfiguration();
Configuration machineConfig = ConfigurationManager.OpenMachineConfiguration();
There are different sections exist in it, let’s check ProtectedConfigurationSection.
ProtectedConfigurationSection pcs =
(ProtectedConfigurationSection)machineSettings.GetSection( "configProtectedData");
Its provider can be displayed as
MessageBox.Show(pcs.DefaultProvider);
Parameters of section can be accessed by.
MessageBox.Show(pcs.Providers[“DataProtectionConfigurationProvider”].Parameters[“description”]);
Configuration section has unique class, and that c
Each configuration section has a unique class. To determine which class a configuration section uses, call ConfigurationManager.OpenMachineConfiguration().GetSection(
"").ElementInformation.Type.ToString.
To edit application configuration file
Create instance of Configuration by method OpenExeConfiguration
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Values can be added as
config.AppSettings.Settings.Add("MyKey", "MyValue");
Save new value to configuration file
config.Save(ConfigurationSaveMode.Modified);
After running above code check your configuration file, it should look like
Application settings can be read as ..
Configuration.AppSettings(“KeyName”);
Above code will return values of “KeyName” key.
Configuration.AppSettings return NameValueCollection, for database connection string we have ConnectionStringCollection.
The three most useful property of ConnectionStringClass are
Name -- defines name of connection string
ProviderName –type of database connection i.e sql servr, oracle, oledb etc
ConnectionString –defines a string how to connect with database.
We have following connection string in configuration file
Integrated Security=SSPI; AttachDBFilename=|DataDirectory|aspnetdb.mdf; User Instance=true"
providerName="System.Data.SqlClient" />
Though in above connections string split in many lines, in actual it can be placed in a single line.
We can access this connection as
ConfigurationManager.ConnectionStrings["mySqlServer"].ConnectionString
To create custom section in our application configuration there are two way to do that in first create a class that inherits from IConfigurationSectionHandler interface and create a class derived from ConfigurationSection class.
No comments:
Post a Comment