Click here to Skip to main content
15,879,095 members
Articles / Programming Languages / C#
Article

INI Class for .NET

Rate me:
Please Sign up or sign in to vote.
3.49/5 (26 votes)
9 Aug 2005CC (ASA 2.5)2 min read 172.3K   1.8K   77   33
Class for interfacing with INI files.

Sample Image - aejw_ini_class.jpg

Introduction

This is a class for reading and adjusting INI files. The class utilizes the WritePrivateProfileString and GetPrivateProfileString API calls. 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 straightforward and more practical to use.

Using the code

  • Add the class file "cINI.cs" to your project / solution.
  • Declare and use the class.
    C#
    //Read value from INI
    aejw.cIni oIni = new aejw.cIni("C:\\INIFile.ini");
    string sRet = oIni.ReadValue("TheSection", 
                       "TheKey", "DefaultReturn");

Functions / Methods

The following examples require the object / class to be declared.

C#
aejw.cIni oIni = new aejw.cIni("C:\\INIFile.ini");

Read Value

C#
//Read value from INI
string sRet = oIni.ReadValue("TheSection", "TheKey", "");

Write Value

C#
//Write value to INI
oIni.WriteValue("TheSection", "TheKey", "TheValue");

Remove Value

C#
//Remove value from INI
oIni.RemoveValue("TheSection", "TheKey");

Read Sections

C#
//Read sections from INI
Array oSections = new Array[0];
oIni.ReadSections(ref oSections);

Read Values (Value names)

C#
//Read values from INI
Array oValues = new Array[0];
oIni.ReadValues(ref oValues);

About the code

Below is a general breakdown of how the code works...

  • 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.)

    C#
    [DllImport("kernel32", SetLastError=true)]
    private static extern int GetPrivateProfileString(
        string pSection,
        string pKey,
        string pDefault,
        byte[] pValue,
        int    pBufferLen,
        string pFile
    );

    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.

  • 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.

    C#
    private string z_GetString(string psSection, 
                               string psKey, string psDefault){
    
        //Prepare the default return and create 
        //a byte array to pass as a pointer
        string sRet=psDefault;
        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(psSection, psKey, 
                        psDefault, bRet, 255, ls_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.
        sRet=System.Text.Encoding.GetEncoding(1252).GetString(bRet, 
                                             0,i).TrimEnd((char)0);
    
        return(sRet);
    
    }
  • Using the function...

    Now that we have a API declaration and friendly function, we can call the function at will...

    C#
    string sSetting=z_GetString("SectionName","ValueName","");
    string sSetting2=z_GetString("SectionName2", 
                                 "ValueName2","DefaultReturn");

History

  • 3rd August 2005 - build 0019
    • Wrote some more general documentation.
    • Changed class filename to 'cINI.cs'.
    • Corrected 'ReadSections' and 'ReadValues'. Rewrote 'z_GetString' function to correctly handle the text encoding and accept chr(0). (Thanks to 'yurij' for reporting the bug).
    • General small bug fixes and improvements.
  • 14th April 2005 - build 0018
    • Corrected issue when returning high ASCII characters, e.g.: 'åäö'
    • Tidied class and documents
  • 6th Feb 2004 - build 0016
    • Corrected bug in 'ReadValue', would return 'char(0)' to the length of the buffer after the value returned.
  • 5th Feb 2004 - build 0015
    • First build posted on CodeProject, based on previous code and dubbed 0015.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


Written By
Web Developer
New Zealand New Zealand
C#, VB.net (Web and forms), SQL Server, MySQL, ASP, Win32 API, ...
Site: aejw.com

Comments and Discussions

 
Questionfirst item of array filled by ReadValues always seems to be an empty string Pin
c_keen12-Mar-12 8:48
c_keen12-Mar-12 8:48 
GeneralThanks for the Class Pin
Decad18-Jul-08 0:34
Decad18-Jul-08 0:34 
GeneralRe: Thanks for the Class Pin
aejw20-Jul-08 14:53
aejw20-Jul-08 14:53 
GeneralIf the value is Unicode,like simple chinese charset, there will be a bug Pin
lwlojj17-Jul-08 4:17
lwlojj17-Jul-08 4:17 
GeneralRe: If the value is Unicode,like simple chinese charset, there will be a bug Pin
aejw20-Jul-08 14:52
aejw20-Jul-08 14:52 
Questionhelp Pin
Member 381377111-Feb-08 3:13
Member 381377111-Feb-08 3:13 
AnswerRe: help Pin
aejw20-Jul-08 14:56
aejw20-Jul-08 14:56 
QuestionHigh ASCII chars in value name Pin
z00z020-Mar-06 7:27
z00z020-Mar-06 7:27 
AnswerRe: High ASCII chars in value name Pin
aejw2-May-06 16:57
aejw2-May-06 16:57 
GeneralRe: High ASCII chars in value name Pin
z00z04-May-06 9:43
z00z04-May-06 9:43 
GeneralNaming convebtions Pin
Guido_d10-Aug-05 0:15
Guido_d10-Aug-05 0:15 
GeneralRe: Naming convebtions Pin
aejw10-Aug-05 7:51
aejw10-Aug-05 7:51 
Thanks... I really enjoyed reading the MSDN article you suggested. I am 100% self trained (thanks to the community) and would say my coding style and naming system is a mix of hundreds programmers that I have studyed / learnt from via code over the years.

I will have a look into changing my naming system before too long (I hope soon anyway, it will be a huge task as I have 100+ classes to convert).

----------------------------
Adam Woods

GeneralUnicode Pin
Elder Benassi3-Aug-05 21:16
Elder Benassi3-Aug-05 21:16 
GeneralRe: Unicode Pin
Anonymous-aejw6-Aug-05 0:49
sussAnonymous-aejw6-Aug-05 0:49 
Generalerror in ReadSections Pin
Member 17274072-Aug-05 10:08
Member 17274072-Aug-05 10:08 
GeneralRe: error in ReadSections Pin
aejw3-Aug-05 11:56
aejw3-Aug-05 11:56 
QuestionNini?? Pin
kaschimer20-Apr-05 13:18
kaschimer20-Apr-05 13:18 
AnswerRe: Nini?? Pin
Yinon Ehrlich14-Jun-05 20:18
Yinon Ehrlich14-Jun-05 20:18 
GeneralNative version Pin
Guido_d20-Apr-05 2:14
Guido_d20-Apr-05 2:14 
GeneralRe: Native version Pin
aejw20-Apr-05 8:28
aejw20-Apr-05 8:28 
GeneralSymbols Pin
Wronex12-Apr-05 6:59
Wronex12-Apr-05 6:59 
GeneralRe: Symbols Pin
aejw14-Apr-05 16:32
aejw14-Apr-05 16:32 
GeneralRe: Symbols Pin
Wronex15-Apr-05 23:42
Wronex15-Apr-05 23:42 
GeneralcINI0015.cs Pin
zoranl16-Mar-04 17:48
zoranl16-Mar-04 17:48 
GeneralRe: cINI0015.cs Pin
aejw17-Mar-04 23:34
aejw17-Mar-04 23:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.