|
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using System.Configuration;
using System.Xml;
namespace ConfigurationSettingsRWSample
{
public class ConfigurationSettingsRWForm : System.Windows.Forms.Form
{
#region Fields
private System.Windows.Forms.DataGrid grdAppSettings;
private System.Data.DataTable tblAppSettings;
private bool bSettingsLoaded;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label lblAppSettings;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnSave;
private System.Windows.Forms.Button btnLoad;
private System.Windows.Forms.DataGrid grdCustomSettings;
private ConfigurationSettingsRW config = null;
#endregion
#region Constructor and Dispose
public ConfigurationSettingsRWForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
string configFilePath = Assembly.GetCallingAssembly().Location + ".config";
config = new ConfigurationSettingsRW(configFilePath);
tblAppSettings = new DataTable("AppSettings");
tblAppSettings.Columns.Add("key", typeof(System.String));
tblAppSettings.Columns.Add("value", typeof(System.String));
bSettingsLoaded = false;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#endregion
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.grdAppSettings = new System.Windows.Forms.DataGrid();
this.lblAppSettings = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.grdCustomSettings = new System.Windows.Forms.DataGrid();
this.btnSave = new System.Windows.Forms.Button();
this.btnLoad = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.grdAppSettings)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.grdCustomSettings)).BeginInit();
this.SuspendLayout();
//
// grdAppSettings
//
this.grdAppSettings.DataMember = "";
this.grdAppSettings.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.grdAppSettings.Location = new System.Drawing.Point(6, 24);
this.grdAppSettings.Name = "grdAppSettings";
this.grdAppSettings.Size = new System.Drawing.Size(280, 128);
this.grdAppSettings.TabIndex = 0;
//
// lblAppSettings
//
this.lblAppSettings.AutoSize = true;
this.lblAppSettings.Location = new System.Drawing.Point(8, 8);
this.lblAppSettings.Name = "lblAppSettings";
this.lblAppSettings.Size = new System.Drawing.Size(64, 16);
this.lblAppSettings.TabIndex = 1;
this.lblAppSettings.Text = "appSettings";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(296, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(82, 16);
this.label1.TabIndex = 3;
this.label1.Text = "customSettings";
//
// grdCustomSettings
//
this.grdCustomSettings.DataMember = "";
this.grdCustomSettings.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.grdCustomSettings.Location = new System.Drawing.Point(296, 24);
this.grdCustomSettings.Name = "grdCustomSettings";
this.grdCustomSettings.Size = new System.Drawing.Size(280, 128);
this.grdCustomSettings.TabIndex = 2;
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(296, 160);
this.btnSave.Name = "btnSave";
this.btnSave.TabIndex = 4;
this.btnSave.Text = "Save";
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
//
// btnLoad
//
this.btnLoad.Location = new System.Drawing.Point(208, 160);
this.btnLoad.Name = "btnLoad";
this.btnLoad.TabIndex = 5;
this.btnLoad.Text = "Load";
this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
//
// ConfigurationSettingsRWForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(582, 191);
this.Controls.Add(this.btnLoad);
this.Controls.Add(this.btnSave);
this.Controls.Add(this.label1);
this.Controls.Add(this.grdCustomSettings);
this.Controls.Add(this.lblAppSettings);
this.Controls.Add(this.grdAppSettings);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "ConfigurationSettingsRWForm";
this.Text = "ConfigurationSettingsRW";
((System.ComponentModel.ISupportInitialize)(this.grdAppSettings)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.grdCustomSettings)).EndInit();
this.ResumeLayout(false);
}
#endregion
#region Main
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new ConfigurationSettingsRWForm());
}
#endregion
#region Specific methods
private void LoadSettings()
{
#region appSettings
// Loading the config.AppSettings from the configuration file.
config.LoadAppSettings();
// Clearing the DataSource of the appSettings DataGrid
grdAppSettings.DataSource = null;
// Creating a DataTable containing the config.AppSettings key-value data.
tblAppSettings.Clear();
for (int i = 0; i < config.AppSettings.Count; i++)
{
string key = config.AppSettings.GetKey(i);
string val = config.AppSettings.Get(i);
DataRow row = tblAppSettings.NewRow();
row["key"] = key;
row["value"] = val;
tblAppSettings.Rows.Add(row);
}
// Assigning the created DataTable to the DataSource of the appSettings DataGrid.
grdAppSettings.DataSource = tblAppSettings;
#endregion
#region customSettings
// Clearing the DataSource of the customSettings DataGrid
grdCustomSettings.DataSource = null;
// Getting a DataTable containing the customSettings data.
DataTable tblCustomSettings = (DataTable)config.GetConfig("customSettings");
// Assigning the DataTable to the DataSource of the customSettings DataGrid.
grdCustomSettings.DataSource = tblCustomSettings;
#endregion
}
private void SaveSettings()
{
#region appSettings
// Recreating the config.AppSettings collection with the data contained in appSettings DataGrid.
config.AppSettings.Clear();
foreach (DataRow row in tblAppSettings.Rows)
config.AppSettings[row["key"].ToString()] = row["value"].ToString();
// Saving the config.AppSettings data into the configuration file.
config.SaveAppSettings();
#endregion
#region customSettings
// Getting the DataTable that is the DataSource of the customSettings DataGrid.
DataTable tblCustomSettings = (DataTable)grdCustomSettings.DataSource;
// Setting the customSettings data to the config.
config.SetConfig(tblCustomSettings, "customSettings");
#endregion
}
#endregion
#region Event handlers
private void btnLoad_Click(object sender, System.EventArgs e)
{
LoadSettings();
bSettingsLoaded = true;
}
private void btnSave_Click(object sender, System.EventArgs e)
{
if (bSettingsLoaded)
SaveSettings();
}
#endregion
}
public class CustomSettingsSectionHandler: IConfigurationRWSectionHandler
{
#region IConfigurationRWSectionHandler Members
public object Create(System.Xml.XmlNode section)
{
DataTable tblCustomSettings = new DataTable("CustomSettings");
tblCustomSettings.Columns.Add("attr1", typeof(System.String));
tblCustomSettings.Columns.Add("attr2", typeof(System.String));
tblCustomSettings.Columns.Add("attr3", typeof(System.String));
foreach (XmlNode field in section.SelectNodes("field"))
{
DataRow row = tblCustomSettings.NewRow();
row["attr1"] = field.Attributes["attr1"].Value;
row["attr2"] = field.Attributes["attr2"].Value;
row["attr3"] = field.Attributes["attr3"].Value;
tblCustomSettings.Rows.Add(row);
}
return tblCustomSettings;
}
public void Persist(object config, System.Xml.XmlNode section)
{
DataTable tblCustomSettings = (DataTable)config;
section.RemoveAll();
foreach (DataRow row in tblCustomSettings.Rows)
{
XmlNode node = section.OwnerDocument.CreateNode(XmlNodeType.Element, "field", "");
XmlAttribute attr = section.OwnerDocument.CreateAttribute("attr1");
attr.Value = row["attr1"].ToString();
node.Attributes.Append(attr);
attr = section.OwnerDocument.CreateAttribute("attr2");
attr.Value = row["attr2"].ToString();
node.Attributes.Append(attr);
attr = section.OwnerDocument.CreateAttribute("attr3");
attr.Value = row["attr3"].ToString();
node.Attributes.Append(attr);
section.AppendChild(node);
}
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.