Click or drag to resize

ConfigurationFile Class

Represents a configuration file of a Windows or Web application.
Inheritance Hierarchy
SystemObject
  GSF.ConfigurationConfigurationFile

Namespace: GSF.Configuration
Assembly: GSF.Core (in GSF.Core.dll) Version: 2.4.181-beta
Syntax
public class ConfigurationFile
View Source

The ConfigurationFile type exposes the following members.

Properties
 NameDescription
Public propertyConfiguration Get the underlying Configuration that can be accessed using this ConfigurationFile object.
Public propertyCulture Gets or sets the CultureInfo to use for the conversion of setting values to and from String.
Public propertyStatic memberCurrent Gets the ConfigurationFile object that represents the config file of the currently executing Windows or Web application.
Public propertySettings Gets the CategorizedSettingsSection object representing settings under the "categorizedSettings" section of the config file.
Top
Methods
 NameDescription
Public methodEqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object)
Public methodGetHashCodeServes as the default hash function.
(Inherited from Object)
Public methodGetTypeGets the Type of the current instance.
(Inherited from Object)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object.
(Inherited from Object)
Public methodStatic memberOpen Opens application config file at the specified configFilePath.
Public methodReload Reloads the current configuration settings from the configuration file that the ConfigurationFile represents.
Public methodRestoreDefaultUserSettings Restores all the default settings for User scoped settings.
Public methodSave Writes the configuration settings contained within this ConfigurationFile object to the configuration file that it represents.
Public methodSave(ConfigurationSaveMode) Writes the configuration settings contained within this ConfigurationFile object to the configuration file that it represents.
Public methodSaveAs Writes the configuration settings contained within this ConfigurationFile object to the specified configuration file.
Public methodSetCryptoKey Sets the key to be used for encrypting and decrypting values of Settings.
Public methodToStringReturns a string that represents the current object.
(Inherited from Object)
Top
Extension Methods
 NameDescription
Public Extension MethodGetEnumValueOrDefault Gets the enumeration constant for value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Public Extension MethodGetEnumValueOrDefaultT Gets the enumeration constant for this value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Top
Example
This example shows how to save and read settings from the config file:
C#
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:
C#
<?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>
See Also