|
|
Configuration
|
The ConfigurationFile type exposes the following members.
| Name | Description | |
|---|---|---|
| Configuration | Get the underlying Configuration that can be accessed using this ConfigurationFile object. | |
| Culture | Gets or sets the CultureInfo to use for the conversion of setting values to and from String. | |
| Current | Gets the ConfigurationFile object that represents the config file of the currently executing Windows or Web application. | |
| Settings | Gets the CategorizedSettingsSection object representing settings under the "categorizedSettings" section of the config file. |
| Name | Description | |
|---|---|---|
| Equals | Determines whether the specified object is equal to the current object. (Inherited from Object) | |
| Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object) | |
| GetHashCode | Serves as the default hash function. (Inherited from Object) | |
| GetType | Gets the Type of the current instance. (Inherited from Object) | |
| MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object) | |
| Open | Opens application config file at the specified configFilePath. | |
| Reload | Reloads the current configuration settings from the configuration file that the ConfigurationFile represents. | |
| RestoreDefaultUserSettings | Restores all the default settings for User scoped settings. | |
| Save | Writes the configuration settings contained within this ConfigurationFile object to the configuration file that it represents. | |
| Save(ConfigurationSaveMode) | Writes the configuration settings contained within this ConfigurationFile object to the configuration file that it represents. | |
| SaveAs | Writes the configuration settings contained within this ConfigurationFile object to the specified configuration file. | |
| SetCryptoKey | Sets the key to be used for encrypting and decrypting values of Settings. | |
| ToString | Returns a string that represents the current object. (Inherited from Object) |
| Name | Description | |
|---|---|---|
| GetEnumValueOrDefault |
Gets the enumeration constant for value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions) | |
| GetEnumValueOrDefaultT |
Gets the enumeration constant for this value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions) |
using System;
using System.Configuration;
using GSF;
using GSF.Configuration;
class Program
{
static void Main(string[] args)
{
// Get the application config file.
ConfigurationFile config = ConfigurationFile.Current;
// Get the sections of config file.
CategorizedSettingsElementCollection startup = config.Settings["Startup"];
CategorizedSettingsElementCollection passwords = config.Settings["Passwords"];
CategorizedSettingsElementCollection monitoring = config.Settings["Monitoring"];
KeyValueConfigurationCollection appSettings = config.Configuration.AppSettings.Settings;
ConnectionStringSettingsCollection connStrings = config.Configuration.ConnectionStrings.ConnectionStrings;
// Add settings to the config file under the "appSettings" section.
appSettings.Add("SaveSettingOnExit", true.ToString());
// Add settings to the config file under the "connectionStrings" section.
connStrings.Add(new ConnectionStringSettings("DevSql", "Server=SqlServer;Database=Sandbox;Trusted_Connection=True"));
// Add settings to the config (if they don't exist) under a custom "monitoring" section.
monitoring.Add("RefreshInterval", 5, "Interval in seconds at which the Monitor screen is to be refreshed.");
monitoring.Add("MessagesSnapshot", 30000, "Maximum messages length to be displayed on the Monitor screen.");
// Add password to the config file encrypted (if it doesn't exist) under a custom "passwords" section.
passwords.Add("Admin", "Adm1nP4ss", "Password used for performing administrative tasks.", true);
// Add user-scope setting to the config (if it doesn't exist) under a custom "startup" section.
startup.Add("Theme", "Default", "Application theme to use for the session.", false, SettingScope.User);
config.Save(); // Save settings to the config file.
// Read saved settings from the config file.
bool saveSettingsOnExit = appSettings["SaveSettingOnExit"].Value.ParseBoolean();
string devConnectionString = connStrings["DevSql"].ConnectionString;
string appTheme = startup["Theme"].Value;
string adminPassword = passwords["Admin"].Value;
int refreshInterval = monitoring["RefreshInterval"].ValueAsInt32();
int messagesSnapshot = monitoring["MessagesSnapshot"].ValueAsInt32();
// Print the retrieved settings to the console.
Console.WriteLine("SaveSettingOnExit = {0}", saveSettingsOnExit);
Console.WriteLine("DevSql = {0}", devConnectionString);
Console.WriteLine("Theme = {0}", appTheme);
Console.WriteLine("Admin = {0}", adminPassword);
Console.WriteLine("RefreshInterval = {0}", refreshInterval);
Console.WriteLine("MessagesSnapshot = {0}", messagesSnapshot);
Console.ReadLine();
}
}
This example shows the content of the config file from the sample code above:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="categorizedSettings" type="GSF.Configuration.CategorizedSettingsSection, GSF.Core" />
</configSections>
<appSettings>
<add key="SaveSettingOnExit" value="True" />
</appSettings>
<categorizedSettings>
<startup>
<add name="Theme" value="Default" description="Application theme to use for the session."
encrypted="false" scope="User" />
</startup>
<passwords>
<add name="Admin" value="C+0j6fE/N0Q9b5xaeDKgvRmSeY9zJkO1EQCr7cHoG3x24tztlbBB54PfWsuMGXc/"
description="Password used for performing administrative tasks."
encrypted="true" />
</passwords>
<monitoring>
<add name="RefreshInterval" value="5" description="Interval in seconds at which the Monitor screen is to be refreshed."
encrypted="false" />
<add name="MessagesSnapshot" value="30000" description="Maximum messages length to be displayed on the Monitor screen."
encrypted="false" />
</monitoring>
</categorizedSettings>
<connectionStrings>
<add name="DevSql" connectionString="Server=SqlServer;Database=Sandbox;Trusted_Connection=True" />
</connectionStrings>
</configuration>