The Configuration class includes a number of methods that allow you to read and write configuration settings in different storage mediums including string, file, or a SQL Server Table field.

The behavior with different stores is slightly different since these stores all use Serialization of the object. Unlike the default Config file behavior, reading from an alternate store creates a new object rather than setting the properties on the current object. All these stores require factory methods to create an instance of the configuration class. For example, to Create a Configruation object from a file:

App.Configuration = (WebStoreConfig) WebStoreConfig.ReadKeysFromFile("d:\\temp\\WebConfig.xml", typeof(WebStoreConfig)) ;

If you're using encryption, note that because a new instance is created here you should make sure that your Constructor sets up the Encryption:

public WebStoreConfig() { this.SetEnryption("ConnectionString,MailPassword,MerchantPassword","WebStorePassword"); }

This ensures that encryption and decryption occurs when new instances are created from other stores.

The following example demonstrate how to use each of the stores available. Remember data is stored as a single XML value in all modes except the Configuration file option.

File Storage


You can write out configuration settings to a custom file on disk. This file contains just the settings of your class. To do this you can use the following code:

// *** Write WebStoreConfig Config = new WebStoreConfig(); WebStoreConfig.WriteKeysToFile("d:\\temp\\WebConfig.xml"); ... // *** Read WebStoreConfig Config = (WebStoreConfig) WebStoreConfig.ReadKeysFromFile("d:\\temp\\WebConfig.xml", typeof(WebStoreConfig)); int ConnectMode = Config.ConnectMode;

String Storage


You can write configuration settings to a string. This gives a few additional options for storing configuration settings in a location of your own choice. For example, you can store the data in the field of a database of your own choice as part of user settings you might want to save.

// *** Write WebStoreConfig Config = new WebStoreConfig(); string XML = ""; Config.WriteKeysToString(out XML); ... // *** Read WebStoreConfig Config = (WebStoreConfig) WebStoreConfig.ReadKeysFromString(XML,typeof(WebStoreConfig)); int ConnectMode = Config.ConnectMode;

Note that the WriteKeysToString() method writes the XML tring into an out parameter for consistency with the other Read methods that return a bool result value.

Sql Server Storage


You can write configuration settings into a database. This mechanism uses a custom configuration table and structure that contains ID and ConfigData fields. The Configuration data is written out as a single string into the ConfigData field.

// *** Write WebStoreConfig Config = new WebStoreConfig(); Config.WriteKeysToSqlServer( "server=(local);Database=WebStore;uid=WebStore;pwd=xxxxxxxx", "wwConfigurationSettings",0); ... // *** Read WebStoreConfig Config = (WebStoreConfig) WebStoreConfig.ReadkeysFromSqlServer( "server=(local);Database=WebStore;uid=WebStore;pwd=wwind", "wwConfigurationSettings",0,typeof(WebStoreConfig)); int ConnectMode = Config.ConnectMode;

Both Read and Write operations require a connection string, table name and a key id in the table. However, the Table or record doesn't have to exist - the class will create a new record and even a table (assuming the application has permissions to do so).

Store Location Keys


Note that both file and Sql Server stores require information about the location of the data (file name and Sql Connection and table info respectively), so you will need to figure out how to store and load this value separately in a place like a single AppSetting in the configuration file.

Encryption with special persistance stores


Encryption also works for these alternate configuration stores, but you should make sure that your encryption Setup (EncryptionFields and Key) is made in the default constructor of your configuration object. For example

public WebStoreConfig() { this.SetEncryption("ConnectionString,MailPassword,MerchantPassword","WebStorePassword"); }

This makes sure that the encryption settings are always applied when calling the various static Read methods that return an object instance. Decryption is applied before the object is returned to you if you have configured any fields to encrypt.


Last Updated: 9/20/2004 | Send topic feedback