Configuring ASP.NET Application with System.Configuration
Configuration Properties and Methods
Name | Description |
---|---|
AppSettings | Gets the AppSettingsSection object |
ConnectionStrings | Gets the ConnectionStrings |
ContextInformation | Gets the ContextInformation |
FilePath | Gets the physical path |
GetSection | Returns the specified ConfigurationSection |
GetSectionGroup | Returns the specified ConfigurationSectionGroup |
HasFile | Indicates whether a configuration file exists |
NamespaceDeclared | Gets or sets a value if it has an XML namespace |
RootSectionGroup | Gets the ConfigurationSectionGroup for this Configuration object |
Save | Writes the configuration settings |
SaveAs | Writes the configuration settings |
ConfigurationManager Properties and Methods
Name | Description |
---|---|
AppSettings | Gets the AppSettingsSection data |
ConnectionStrings | Gets the ConnectionStringsSection data |
GetSection | Retrieves a specified configuration section |
OpenExeConfiguration | Opens the specified client configuration |
OpenMachineConfiguration | Opens the Machine Configuration file |
OpenMappedExeConfiguration | Opens the specified client configuration |
OpenMappedMachineConfiguration | Opens the specified client configuration |
//Open app. file
System.Configuration myConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//Open machine file
ExeConfigurationFileMap MyMap = new ExeConfigurationFileMap();
MyMap.ExeConfigFilename = @"DBStringOneDemo.exe.config";
System.Configuration myConfiguration = ConfigurationManager.OpenMappedMachineConfiguration(MyMap);
ConfigurationUserLevel Enumeration
Name | Description |
---|---|
None | Gets the System.Configuration.Configuration |
PerUserRoaming | Gets the roaming System.Configuration.Configuration |
PerUserRoamingAndLocal | Gets the local System.Configuration.Configuration |
There are several common sense tasks that can be hard coded into configuration file and later on retrieved by the application in an object oriented way. They are:
- supportedRuntime – determines version of the application.
- developmentMode – manages GAC for Assembly install/uninstall via DEVPATH. It instructs the CLR to use DEVPATH.
- codeBase – specifies Assembly in the GAC that we want to use if multiple assemblies are installed in our GAC, we need to specify version and href inside this tag for location.
- connectionStrings and appSettings – our configuration string for access DB.
<connectionStrings>
<clear/>
<addname="AdventureWorksString"
providerName="System.Data.SqlClient"
connectionString="Data Source=localhost;Initial
Catalog=AdventureWorksDB; Integrated Security=true"/>
</connectionString>Retrieval of connection information is done this way.
String HelloWorldVariable = ConfigurationSettings.AppSettings["Fool"];
There is newer way to access AppSetting and it’s done like that
NameValueCollection MyAppSettings = ConfigurationManager.AppSettings;
Console.WriteLine(MyAppSettings["Fool"]);
Console.WriteLine(MyAppSettings[0]);
In addition AppSetting implement IEnumerable interface that allows having collection of items and traversing them easily.