Click here to Skip to main content
6,295,667 members and growing! (12,710 online)
Email Password   helpLost your password?
Desktop Development » Files and Folders » Configuration Files     Intermediate License: The Creative Commons Attribution-ShareAlike 2.5 License

INI Class for .NET

By aejw

Class for interfacing with INI files.
C#.NET 1.0, .NET 1.1, Win2K, WinXP, Win2003VS.NET2003, Dev
Posted:4 Feb 2004
Updated:9 Aug 2005
Views:92,099
Bookmarked:63 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
24 votes for this article.
Popularity: 4.51 Rating: 3.26 out of 5
8 votes, 33.3%
1

2
2 votes, 8.3%
3
3 votes, 12.5%
4
11 votes, 45.8%
5

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

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

Read Value

//Read value from INI

string sRet = oIni.ReadValue("TheSection", "TheKey", "");

Write Value

//Write value to INI

oIni.WriteValue("TheSection", "TheKey", "TheValue");

Remove Value

//Remove value from INI

oIni.RemoveValue("TheSection", "TheKey");

Read Sections

//Read sections from INI

Array oSections = new Array[0];
oIni.ReadSections(ref oSections);

Read Values (Value names)

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

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

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

    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

About the Author

aejw


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

Other popular Files and Folders articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 32 (Total in Forum: 32) (Refresh)FirstPrevNext
GeneralThanks for the Class PinmemberDecad1:34 18 Jul '08  
GeneralRe: Thanks for the Class Pinmemberaejw15:53 20 Jul '08  
GeneralIf the value is Unicode,like simple chinese charset, there will be a bug Pinmemberlwlojj5:17 17 Jul '08  
GeneralRe: If the value is Unicode,like simple chinese charset, there will be a bug Pinmemberaejw15:52 20 Jul '08  
Questionhelp PinmemberMember 38137714:13 11 Feb '08  
AnswerRe: help Pinmemberaejw15:56 20 Jul '08  
QuestionHigh ASCII chars in value name Pinmemberz00z08:27 20 Mar '06  
AnswerRe: High ASCII chars in value name Pinmemberaejw17:57 2 May '06  
GeneralRe: High ASCII chars in value name Pinmemberz00z010:43 4 May '06  
GeneralNaming convebtions PinmemberGuido_d1:15 10 Aug '05  
GeneralRe: Naming convebtions Pinmemberaejw8:51 10 Aug '05  
GeneralUnicode PinmemberElder Benassi22:16 3 Aug '05  
GeneralRe: Unicode PinsussAnonymous-aejw1:49 6 Aug '05  
Generalerror in ReadSections Pinmemberyurij11:08 2 Aug '05  
GeneralRe: error in ReadSections Pinmemberaejw12:56 3 Aug '05  
GeneralNini?? PinmemberRabidKangaroo14:18 20 Apr '05  
GeneralRe: Nini?? PinmemberYinon Ehrlich21:18 14 Jun '05  
GeneralNative version PinmemberGuido_d3:14 20 Apr '05  
GeneralRe: Native version Pinmemberaejw9:28 20 Apr '05  
GeneralSymbols PinmemberSpeedhawk7:59 12 Apr '05  
GeneralRe: Symbols Pinmemberaejw17:32 14 Apr '05  
GeneralRe: Symbols PinmemberSpeedhawk0:42 16 Apr '05  
GeneralcINI0015.cs Pinmemberzoranl18:48 16 Mar '04  
GeneralRe: cINI0015.cs Pinmemberaejw0:34 18 Mar '04  
GeneralRe: cINI0015.cs Pinmemberzoranl18:58 18 Mar '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Aug 2005
Editor: Smitha Vijayan
Copyright 2004 by aejw
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project