Hi this is a small class i made for C# to be-able to read and write to ini files, Please note I only started to learn C# this week,, and most of my experince has come form VB. So if you see any errors in my code you think I chould have done something different please let me know cos it will help me on other projects. anyway I hope you find it usfull.
Class code cIni.cs
Code:
//A simple INI class for C# by Ben Jones
// Filename the ini file you want to open
// SetValue sets a new ini key value
// GetValue gets a keys value
// DeleteSelection will delete a selection includeing keys and values
// DeleteKey will delete a key and it's value
// GetSelections will return selections names into an arraylist
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections;
namespace INIReader
{
class cINI
{
public string Filename;
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,
string key, string val, string Filename);
[DllImport("kernel32")]
private static extern long GetPrivateProfileString(string section,
string key, string DefaultValue, StringBuilder retVal,
int size, string Filename);
public string GetValue(string Selection, string KeyName, string DefaultValue)
{
//Read INI value
long RetVal;
//Create buffer to hold value
StringBuilder str = new StringBuilder(260);
//Get value form ini file
RetVal = GetPrivateProfileString(Selection, KeyName, DefaultValue, str, 260, this.Filename);
//Return the string
return str.ToString();
}
public long SetValue(string Selection, string KeyName, string KeyValue)
{
//Write new value to ini file
return WritePrivateProfileString(Selection, KeyName, KeyValue, this.Filename);
}
public long DeleteSelection(string Selection)
{
//Delete a selection form ini
return WritePrivateProfileString(Selection, null, null, this.Filename);
}
public long DeleteKey(string Selection, string KeyName)
{
//Deletes a key and it's value
return WritePrivateProfileString(Selection, KeyName, null, this.Filename);
}
public ArrayList GetSelections()
{
string sline = "";
ArrayList list = new ArrayList();
StreamReader sr = new StreamReader(this.Filename);
while (sline != null)
{
sline = sr.ReadLine();
//Read while we no null
if (sline != null)
{ //Check for a vaild length
if (Convert.ToBoolean(sline.Length))
{
//Check for opening bracket [
if (sline.Substring(0, 1) == "[")
{
//Check for closeing bracket ]
if (sline.Substring(sline.Length - 1, 1) == "]")
{
//Extract the selection name and add to the arraylist
list.Add(sline.Substring(1,sline.Length-2));
}
}
}
}
}
return list;
list.Clear();
sr.Close();
}
}
}
Example
Code:
//Example ini file
// [general]
// TestKey=Some Value
// [main]
// Color=Red
//Example project
//Note you need to add using System.Collections; to the top of your form
//Declare a new MyIniFile class
cINI MyIniFile;
//Create new Array List
ArrayList list = new ArrayList();
//Create new Ini Object
MyIniFile = new cINI();
//Filename to load
MyIniFile.Filename = "C:\\cd\\test.ini";
MessageBox.Show(MyIniFile.GetValue("general", "TestKey", "error"));
//Get all the ini selections
list = MyIniFile.GetSelections();
//Loop tho and display the selections
foreach (string item in list)
{
//Show selection names
MessageBox.Show(item);
}
//Clear up time
list.Clear();