Click here to Skip to main content
15,895,746 members
Articles / Programming Languages / C#

Ini Handler

Rate me:
Please Sign up or sign in to vote.
4.00/5 (24 votes)
2 Jun 20042 min read 102.1K   2K   53  
Permits simple access to ini files
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using IniHandler;

namespace IniHandlerDemo
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#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.listCat = new System.Windows.Forms.ListBox();
			this.listKey = new System.Windows.Forms.ListBox();
			this.tValue = new System.Windows.Forms.TextBox();
			this.tCat = new System.Windows.Forms.TextBox();
			this.bCatAdd = new System.Windows.Forms.Button();
			this.lCat = new System.Windows.Forms.Label();
			this.lKeys = new System.Windows.Forms.Label();
			this.bKeyAdd = new System.Windows.Forms.Button();
			this.tKey = new System.Windows.Forms.TextBox();
			this.lValue = new System.Windows.Forms.Label();
			this.bDelCat = new System.Windows.Forms.Button();
			this.bDelKey = new System.Windows.Forms.Button();
			this.bRefresh = new System.Windows.Forms.Button();
			this.bRenameCat = new System.Windows.Forms.Button();
			this.bRenameKey = new System.Windows.Forms.Button();
			this.bModifyValue = new System.Windows.Forms.Button();
			this.tFileName = new System.Windows.Forms.TextBox();
			this.bReadIni = new System.Windows.Forms.Button();
			this.bSave = new System.Windows.Forms.Button();
			this.tComment = new System.Windows.Forms.TextBox();
			this.lComment = new System.Windows.Forms.Label();
			this.bTest = new System.Windows.Forms.Button();
			this.lFileName = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// listCat
			// 
			this.listCat.Location = new System.Drawing.Point(8, 56);
			this.listCat.Name = "listCat";
			this.listCat.Size = new System.Drawing.Size(156, 186);
			this.listCat.TabIndex = 0;
			this.listCat.SelectedIndexChanged += new System.EventHandler(this.listCat_SelectedIndexChanged);
			// 
			// listKey
			// 
			this.listKey.Location = new System.Drawing.Point(176, 56);
			this.listKey.Name = "listKey";
			this.listKey.Size = new System.Drawing.Size(176, 186);
			this.listKey.TabIndex = 1;
			this.listKey.SelectedIndexChanged += new System.EventHandler(this.listKey_SelectedIndexChanged);
			// 
			// tValue
			// 
			this.tValue.Location = new System.Drawing.Point(360, 56);
			this.tValue.Name = "tValue";
			this.tValue.Size = new System.Drawing.Size(192, 20);
			this.tValue.TabIndex = 2;
			this.tValue.Text = "";
			// 
			// tCat
			// 
			this.tCat.Location = new System.Drawing.Point(8, 248);
			this.tCat.Name = "tCat";
			this.tCat.Size = new System.Drawing.Size(104, 20);
			this.tCat.TabIndex = 3;
			this.tCat.Text = "";
			// 
			// bCatAdd
			// 
			this.bCatAdd.Location = new System.Drawing.Point(120, 248);
			this.bCatAdd.Name = "bCatAdd";
			this.bCatAdd.Size = new System.Drawing.Size(44, 20);
			this.bCatAdd.TabIndex = 4;
			this.bCatAdd.Text = "Add";
			this.bCatAdd.Click += new System.EventHandler(this.bCatAdd_Click);
			// 
			// lCat
			// 
			this.lCat.Location = new System.Drawing.Point(8, 36);
			this.lCat.Name = "lCat";
			this.lCat.Size = new System.Drawing.Size(80, 16);
			this.lCat.TabIndex = 5;
			this.lCat.Text = "Categories";
			// 
			// lKeys
			// 
			this.lKeys.Location = new System.Drawing.Point(176, 32);
			this.lKeys.Name = "lKeys";
			this.lKeys.Size = new System.Drawing.Size(40, 16);
			this.lKeys.TabIndex = 5;
			this.lKeys.Text = "Keys";
			// 
			// bKeyAdd
			// 
			this.bKeyAdd.Location = new System.Drawing.Point(304, 248);
			this.bKeyAdd.Name = "bKeyAdd";
			this.bKeyAdd.Size = new System.Drawing.Size(48, 20);
			this.bKeyAdd.TabIndex = 4;
			this.bKeyAdd.Text = "Add";
			this.bKeyAdd.Click += new System.EventHandler(this.bKeyAdd_Click);
			// 
			// tKey
			// 
			this.tKey.Location = new System.Drawing.Point(180, 248);
			this.tKey.Name = "tKey";
			this.tKey.Size = new System.Drawing.Size(116, 20);
			this.tKey.TabIndex = 3;
			this.tKey.Text = "";
			// 
			// lValue
			// 
			this.lValue.Location = new System.Drawing.Point(360, 32);
			this.lValue.Name = "lValue";
			this.lValue.Size = new System.Drawing.Size(40, 12);
			this.lValue.TabIndex = 5;
			this.lValue.Text = "Value";
			// 
			// bDelCat
			// 
			this.bDelCat.Location = new System.Drawing.Point(8, 276);
			this.bDelCat.Name = "bDelCat";
			this.bDelCat.Size = new System.Drawing.Size(72, 20);
			this.bDelCat.TabIndex = 4;
			this.bDelCat.Text = "Delete";
			this.bDelCat.Click += new System.EventHandler(this.bDelCat_Click);
			// 
			// bDelKey
			// 
			this.bDelKey.Location = new System.Drawing.Point(180, 276);
			this.bDelKey.Name = "bDelKey";
			this.bDelKey.Size = new System.Drawing.Size(88, 20);
			this.bDelKey.TabIndex = 4;
			this.bDelKey.Text = "Delete";
			this.bDelKey.Click += new System.EventHandler(this.bDelKey_Click);
			// 
			// bRefresh
			// 
			this.bRefresh.Location = new System.Drawing.Point(140, 4);
			this.bRefresh.Name = "bRefresh";
			this.bRefresh.Size = new System.Drawing.Size(72, 24);
			this.bRefresh.TabIndex = 6;
			this.bRefresh.Text = "Refresh";
			this.bRefresh.Click += new System.EventHandler(this.bRefresh_Click);
			// 
			// bRenameCat
			// 
			this.bRenameCat.Location = new System.Drawing.Point(92, 276);
			this.bRenameCat.Name = "bRenameCat";
			this.bRenameCat.Size = new System.Drawing.Size(72, 20);
			this.bRenameCat.TabIndex = 4;
			this.bRenameCat.Text = "Rename";
			this.bRenameCat.Click += new System.EventHandler(this.bRenameCat_Click);
			// 
			// bRenameKey
			// 
			this.bRenameKey.Location = new System.Drawing.Point(276, 276);
			this.bRenameKey.Name = "bRenameKey";
			this.bRenameKey.Size = new System.Drawing.Size(76, 20);
			this.bRenameKey.TabIndex = 4;
			this.bRenameKey.Text = "Rename";
			this.bRenameKey.Click += new System.EventHandler(this.bRenameKey_Click);
			// 
			// bModifyValue
			// 
			this.bModifyValue.Location = new System.Drawing.Point(360, 80);
			this.bModifyValue.Name = "bModifyValue";
			this.bModifyValue.Size = new System.Drawing.Size(68, 20);
			this.bModifyValue.TabIndex = 4;
			this.bModifyValue.Text = "Modify";
			this.bModifyValue.Click += new System.EventHandler(this.bModifyValue_Click);
			// 
			// tFileName
			// 
			this.tFileName.Location = new System.Drawing.Point(12, 340);
			this.tFileName.Name = "tFileName";
			this.tFileName.Size = new System.Drawing.Size(144, 20);
			this.tFileName.TabIndex = 2;
			this.tFileName.Text = "";
			// 
			// bReadIni
			// 
			this.bReadIni.Location = new System.Drawing.Point(164, 340);
			this.bReadIni.Name = "bReadIni";
			this.bReadIni.Size = new System.Drawing.Size(64, 20);
			this.bReadIni.TabIndex = 4;
			this.bReadIni.Text = "Read Ini";
			this.bReadIni.Click += new System.EventHandler(this.bReadIni_Click);
			// 
			// bSave
			// 
			this.bSave.Location = new System.Drawing.Point(236, 340);
			this.bSave.Name = "bSave";
			this.bSave.Size = new System.Drawing.Size(64, 20);
			this.bSave.TabIndex = 4;
			this.bSave.Text = "Save Ini";
			this.bSave.Click += new System.EventHandler(this.bSave_Click);
			// 
			// tComment
			// 
			this.tComment.Location = new System.Drawing.Point(360, 244);
			this.tComment.Multiline = true;
			this.tComment.Name = "tComment";
			this.tComment.Size = new System.Drawing.Size(192, 116);
			this.tComment.TabIndex = 7;
			this.tComment.Text = "This comment will be added when saving";
			// 
			// lComment
			// 
			this.lComment.Location = new System.Drawing.Point(360, 224);
			this.lComment.Name = "lComment";
			this.lComment.Size = new System.Drawing.Size(88, 16);
			this.lComment.TabIndex = 5;
			this.lComment.Text = "Comment:";
			// 
			// bTest
			// 
			this.bTest.Location = new System.Drawing.Point(16, 4);
			this.bTest.Name = "bTest";
			this.bTest.Size = new System.Drawing.Size(104, 24);
			this.bTest.TabIndex = 6;
			this.bTest.Text = "Create Test ini";
			this.bTest.Click += new System.EventHandler(this.bTest_Click);
			// 
			// lFileName
			// 
			this.lFileName.Location = new System.Drawing.Point(12, 316);
			this.lFileName.Name = "lFileName";
			this.lFileName.Size = new System.Drawing.Size(152, 16);
			this.lFileName.TabIndex = 5;
			this.lFileName.Text = "File Saving and Reading";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(556, 365);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.tComment,
																		  this.bRefresh,
																		  this.lCat,
																		  this.bCatAdd,
																		  this.tCat,
																		  this.tValue,
																		  this.listKey,
																		  this.listCat,
																		  this.lKeys,
																		  this.bKeyAdd,
																		  this.tKey,
																		  this.lValue,
																		  this.bDelCat,
																		  this.bDelKey,
																		  this.bRenameCat,
																		  this.bRenameKey,
																		  this.bModifyValue,
																		  this.tFileName,
																		  this.bReadIni,
																		  this.bSave,
																		  this.lComment,
																		  this.bTest,
																		  this.lFileName});
			this.Name = "Form1";
			this.Text = "IniHandler Demo";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private System.Windows.Forms.ListBox listCat;
		private System.Windows.Forms.ListBox listKey;
		private System.Windows.Forms.TextBox tValue;
		private System.Windows.Forms.TextBox tCat;
		private System.Windows.Forms.Button bCatAdd;
		private System.Windows.Forms.Label lCat;
		private System.Windows.Forms.Label lKeys;
		private System.Windows.Forms.Button bKeyAdd;
		private System.Windows.Forms.TextBox tKey;
		private System.Windows.Forms.Label lValue;
		private System.Windows.Forms.Button bDelCat;
		private System.Windows.Forms.Button bDelKey;
		private System.Windows.Forms.Button bRefresh;
		private System.Windows.Forms.Button bRenameCat;
		private System.Windows.Forms.Button bRenameKey;
		private System.Windows.Forms.Button bModifyValue;
		private System.Windows.Forms.TextBox tFileName;
		private System.Windows.Forms.Button bReadIni;

		IniStructure inis = new IniStructure();
		private System.Windows.Forms.Button bSave;
		private System.Windows.Forms.TextBox tComment;
		private System.Windows.Forms.Label lComment;
		private System.Windows.Forms.Label lFileName;
		private System.Windows.Forms.Button bTest;

		private void Form1_Load(object sender, System.EventArgs e)
		{
		}

		private new void Refresh()
		{
			tValue.Text ="";
			listCat.Items.Clear();
			listKey.Items.Clear();
			foreach (string name in inis.GetCategories())
			{
				listCat.Items.Add(name);
			}
		}

		private void bCatAdd_Click(object sender, System.EventArgs e)
		{
			if (!inis.AddCategory(tCat.Text))
				MessageBox.Show("AddCategory returned false","error");
			Refresh();
		}

		private void bKeyAdd_Click(object sender, System.EventArgs e)
		{
			if (GetCatName() == "")
			{
				MessageBox.Show("You have to select a category","error");
				return;
			}			if (!inis.AddValue(GetCatName(), tKey.Text, tValue.Text))
							MessageBox.Show("AddValue returned false","error");
			Refresh();
			
		}

		private void listCat_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			listKey.Items.Clear();
			string[] Keys = inis.GetKeys(GetCatName());
			if (Keys == null)
				return;
			tValue.Text = "";
			foreach (string key in Keys )
			{
				listKey.Items.Add(key);
			}
		}

		private void bRefresh_Click(object sender, System.EventArgs e)
		{
			tValue.Text = "";
			Refresh();
		}

		private void listKey_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			
			tValue.Text  = "";
			tValue.Text = inis.GetValue(GetCatName(), GetKeyName());
		}

		private void bDelCat_Click(object sender, System.EventArgs e)
		{
			if (!inis.DeleteCategory(GetCatName()))
				MessageBox.Show("DeleteCategory returned false","error");
			Refresh();
		}

		private void bDelKey_Click(object sender, System.EventArgs e)
		{
			if (GetCatName() == "" | GetKeyName() == "")
			{
				MessageBox.Show("You have to select a category","error");
				return;
			}
			if (!inis.DeleteValue(GetCatName(), GetKeyName()))
				MessageBox.Show("DeleteValue returned false","error");
			Refresh();
		}

		private void bRenameCat_Click(object sender, System.EventArgs e)
		{
			if (!inis.RenameCategory(GetCatName(), tCat.Text))
				MessageBox.Show("RenameCategory returned false","error");
			Refresh();
		}

		private string GetCatName()
		{
			if (listCat.SelectedIndex == -1)
				return "";
			return listCat.Items[listCat.SelectedIndex].ToString();
		}

		private string GetKeyName()
		{
			if (listKey.SelectedIndex == -1)
				return null;
			return listKey.Items[listKey.SelectedIndex].ToString();
		}

		private void bRenameKey_Click(object sender, System.EventArgs e)
		{
			if (GetCatName() == ""| GetKeyName() == "")
			{
				MessageBox.Show("You have to select a category","error");
				return;
			}


			if (!inis.RenameKey(GetCatName(), GetKeyName(), tKey.Text))
				MessageBox.Show("RenameKey returned false","error");
			Refresh();
		}

		private void bModifyValue_Click(object sender, System.EventArgs e)
		{
			if (GetCatName() == "" | GetKeyName() == "")
			{
				MessageBox.Show("You have to select a category","error");
				return;
			}
			if (!inis.ModifyValue(GetCatName(), GetKeyName(), tValue.Text))
				MessageBox.Show("ModifyValue returned false","error");
			Refresh();
		}

		private void bReadIni_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog dialog = new OpenFileDialog();
			dialog.Filter = "Ini files (*.ini)|*.ini|All files (*.*)|*.*";
			dialog.InitialDirectory = @"C:\";
			dialog.RestoreDirectory = true;
			
			if (dialog.ShowDialog() == DialogResult.OK)
			{
				tFileName.Text = dialog.FileName;

				inis = IniStructure.ReadIni(tFileName.Text);
				if (inis == null)
					MessageBox.Show("Something went wrong","error");	
				Refresh();
			}
		}

		private void bSave_Click(object sender, System.EventArgs e)
		{
			SaveFileDialog dialog = new SaveFileDialog();
			dialog.Filter = "Ini files (*.ini)|*.ini|All files (*.*)|*.*";
			dialog.DefaultExt = "ini";
			dialog.RestoreDirectory = true;
			dialog.InitialDirectory = @"C:\";
			if (dialog.ShowDialog() == DialogResult.OK)
			{
				tFileName.Text = dialog.FileName;

				if (!IniStructure.WriteIni(inis,tFileName.Text,tComment.Text))
					MessageBox.Show("Something went wrong","error");
			}
		}

		private void bTest_Click(object sender, System.EventArgs e)
		{
			// nothing is written to disk
			inis.AddCategory("Global");
			inis.AddValue("Global","first key","Key with space");
			inis.AddValue("Global","new","43");
			inis.AddValue("Global","old","Jack");

			inis.AddCategory("Other");
			inis.AddValue("Other","greeting","Hello everybody");
			inis.AddValue("Other","new","352");
			inis.AddValue("Other","Empty","");

			inis.AddCategory("NewSection");
			inis.AddValue("NewSection","NewKey","value");
			inis.ModifyValue("NewSection","NewKey","Newvalue");

			Refresh();
			
		}
	}
}

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Switzerland Switzerland
I left the Microsoft world, and am now working with linux and a BSD. This switch was a big relief, mostly for my keyboard, which doesn't get slammed again.

A big thank you to those who helped me with their good articles.

Comments and Discussions