aejw.com
search: 
sign-in - sitemap
nav:// Development Resources » C#, VB.net, ASP.net... Code » cINI - Legacy INI and INF Class (C#)

cINI API Wrapper (C#)

Written mainly for legacy support, as a lot applications still use ".ini" files and although there is XML support native to .NET (".config" files), I tend to find that for some circumstances ini files are straight forward and more practical to use.
Requires: 
Windows 98se - Windows 2003
Language: 
C# (Microsoft Framework 1.0-2.0)
Release: 
build 0022 (Feburary 2006)
cIni Screenshot

Class / Files

Preview Code Download (C# .net 1.1)

Also Published On...

CodeProject.com

Share Article

Using the Code

First add the class file "cIni.cs" to your project / solution, then declare and use the class as desired.
// read value from ini / inf
aejw.cIni ini = new aejw.cIni("C:\\IniFile.ini");
string retValue = ini.ReadValue("TheSection","TheKey", "DefaultReturn");

Reading, Writing and Removing Values

Reading, writing and removing using a ini / inf file...
aejw.cIni ini = new aejw.cIni("C:\\IniFile.ini");

// read value from ini / inf
string retValue = ini.ReadValue("TheSection", "TheKey", "");

// write value to ini / inf
ini.WriteValue("TheSection", "TheKey", "TheValue");

// remove / delete value from ini / inf
ini.RemoveValue("TheSection", "TheKey");

Indexing / Content

Reading Section names and Key names...
aejw.cIni ini = new aejw.cIni("C:\\IniFile.ini");

// read sections from ini / inf
foreach(string sectionName in ini.ReadSections()){
	MessageBox.Show("Section: " + sectionName);
}

// read key names from ini / inf
foreach(string keyName in ini.ReadKeys()){
	MessageBox.Show("Key: " + keyName);
}

Code Tutorial - Referring to the API function

Below is a general breakdown of the method of the code contained in cIni class...

Referring to the API function...
All the functions at some stage use a API call. By reading the MSDN Platform SDK material, we get the C++ API call and behavior of the API function...
DWORD GetPrivateProfileString(
	LPCTSTR lpAppName,
	LPCTSTR lpKeyName,
	LPCTSTR lpDefault,
	LPTSTR  lpReturnedString,
	DWORD   nSize,
	LPCTSTR lpFileName
);
Which loosely translates to... (I am using the term loosely on the bases that there are multiple ways of handling the parameters.)
[DllImport("kernel32", SetLastError=true)]
private static extern int getPrivateProfileString(
	string sectionName,
	string keyName,
	string defaultValue,
	byte[] iniValue,
	int    bufferLen,
	string filename
);
Make note of the "LPTSTR" to "byte[]" conversion. The "LPTSTR" refers to a pointer / reference in memory that the API function will write to, we are going to use a framework function later to collect the text.

Code Tutorial - Calling the API Function

Once the API declaration has been added, we need to create a function to wrap the API function into a C# friendly function.
private string getString(string iniFilename, string section, string key, string defaultValue){

	// prepare the default return and create
	// a byte array to pass as a pointer
	string retValue=defaultValue;
	byte[] bRet=new byte[255];

	// call the API function, passing a Section name, value name, default
	// value, buffer length (length of the passed byte array) and the ini
	// filename.
	int i = GetPrivateProfileString(section, key, defaultValue, bRet, 255, iniFilename);

	// as referred above we use a framework function to collect the string
	// from the byte array used as a pointer. Note the 'i' variable is the
	// return from the API call, this is passed to the text encoding as the
	// total length of the return.
	retValue = System.Text.Encoding.GetEncoding(1252).GetString(bRet,0,i).TrimEnd((char)0);

	return retValue;

}
Using the function... now that we have a API declaration and friendly function, we can call the function at will...
string setting  = getString("SectionName","ValueName","");

string setting2 = getString("SectionName2","ValueName2","DefaultReturn");


100% NZ ©2010 aejw.com - some rights reserved
site map - w3c xhtml1 - w3c css2 - rss2 - atom1
last modified: 2007-11-18 12:54:16

User Pages / Services
  Password Reminder