Click here to Skip to main content
Click here to Skip to main content

An INI file handling class using C#

By , 14 Mar 2002
 

Introduction

I created a C# class Ini which exposes 2 functions from KERNEL32.dll. These functions are: WritePrivateProfileString and GetPrivateProfileString

Namespaces you will need: System.Runtime.InteropServices and System.Text

The Class

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Ini
{
    /// <summary>
    /// Create a New INI file to store or load data
    /// </summary>
    public class IniFile
    {
        public string path;

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section,
            string key,string val,string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section,
                 string key,string def, StringBuilder retVal,
            int size,string filePath);

        /// <summary>
        /// INIFile Constructor.
        /// </summary>
        /// <PARAM name="INIPath"></PARAM>
        public IniFile(string INIPath)
        {
            path = INIPath;
        }
        /// <summary>
        /// Write Data to the INI File
        /// </summary>
        /// <PARAM name="Section"></PARAM>
        /// Section name
        /// <PARAM name="Key"></PARAM>
        /// Key Name
        /// <PARAM name="Value"></PARAM>
        /// Value Name
        public void IniWriteValue(string Section,string Key,string Value)
        {
            WritePrivateProfileString(Section,Key,Value,this.path);
        }
        
        /// <summary>
        /// Read Data Value From the Ini File
        /// </summary>
        /// <PARAM name="Section"></PARAM>
        /// <PARAM name="Key"></PARAM>
        /// <PARAM name="Path"></PARAM>
        /// <returns></returns>
        public string IniReadValue(string Section,string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section,Key,"",temp, 
                                            255, this.path);
            return temp.ToString();

        }
    }
}

Using the class

Steps to use the Ini class:

  1. In your project namespace definition add 
    using INI;
  2. Create a INIFile like this
    INIFile ini = new INIFile("C:\\test.ini");
  3. Use IniWriteValue to write a new value to a specific key in a section or use IniReadValue to read a value FROM a key in a specific Section.

That's all. It's very easy in C# to include API functions in your class(es).

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

About the Author

BLaZiNiX
Web Developer
Canada Canada
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberjai_magical16 Apr '13 - 11:40 
Easy to understand.
GeneralMy vote of 5memberSaravanan.rex5 Sep '12 - 4:00 
my long time issue of saving config solved Smile | :)
GeneralRe: My vote of 5memberHeadzup27 Sep '12 - 2:16 
for me it dont work on Windows 8 Frown | :( the Program don't create the ini file
GeneralRe: My vote of 5memberSaravanan.rex27 Sep '12 - 7:23 
i am using windows 7. please check the file path . use absolute path like @c:\temp.ini. if you put only file name then it's not creating ini file.
QuestionFormClosingmemberpaphnuty27 Aug '12 - 21:38 
The Closing event is obsolete in the .NET Framework version 2.0; use the FormClosing event in example instead.
Questionmy vote of 5membervelt_99124 Apr '12 - 21:09 
this is awesome..very comprehensive...thanks...
 

and easy to use..Cheers!
QuestionIf you want to support unicode, use this.memberchozekun12 Mar '12 - 21:11 
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
 
/// <summary>
/// Create a New INI file to store or load data
/// </summary>
public class IniFile
{
    [DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileStringW",
    SetLastError = true,
    CharSet = CharSet.Unicode, ExactSpelling = true,
    CallingConvention = CallingConvention.StdCall)]
    private static extern int GetPrivateProfileString(
      string lpSection,
      string lpKey,
      string lpDefault,
      StringBuilder lpReturnString,
      int nSize,
      string lpFileName);
 
    [DllImport("KERNEL32.DLL", EntryPoint = "WritePrivateProfileStringW",
      SetLastError = true,
      CharSet = CharSet.Unicode, ExactSpelling = true,
      CallingConvention = CallingConvention.StdCall)]
    private static extern int WritePrivateProfileString(
      string lpSection,
      string lpKey,
      string lpValue,
      string lpFileName);
 
    private string _path = "";
    public string Path {
        get
        {
            return _path;
        }
        set
        {
            if (!File.Exists(value))
                File.WriteAllText(value, "", Encoding.Unicode);
            _path = value;
        }
    }
 
    /// <summary>
    /// INIFile Constructor.
    /// </summary>
    /// <PARAM name="INIPath"></PARAM>
    public IniFile(string INIPath)
    {
        this.Path = INIPath;
    }
 
    /// <summary>
    /// Write Data to the INI File
    /// </summary>
    /// <PARAM name="Section"></PARAM>
    /// Section name
    /// <PARAM name="Key"></PARAM>
    /// Key Name
    /// <PARAM name="Value"></PARAM>
    /// Value Name
    public void IniWriteValue(string Section, string Key, string Value)
    {
        WritePrivateProfileString(Section, Key, Value, this.Path);
    }
 
    /// <summary>
    /// Read Data Value From the Ini File
    /// </summary>
    /// <PARAM name="Section"></PARAM>
    /// <PARAM name="Key"></PARAM>
    /// <PARAM name="Path"></PARAM>
    /// <returns></returns>
    public string IniReadValue(string Section, string Key)
    {
        const int MAX_CHARS = 1023;
        StringBuilder result = new StringBuilder(MAX_CHARS);
        GetPrivateProfileString(Section, Key, "", result, MAX_CHARS, this.Path);
        return result.ToString();
    }
}

GeneralMy vote of 5memberkoiser13 Feb '12 - 3:51 
Clear structured an easy to use!
GeneralMy vote of 5membersanket_14@rediff.com1 Nov '11 - 20:40 
Thankx Dear...!!! It helps me lot...!!!
 
But i hav one query....if i want to use <> this for defining Section i.e.
 
how to read this test.ini file?
 

=
GeneralMy vote of 5memberAli Fakoor21 Oct '11 - 21:44 
very creative article
QuestionRead and write ini files in VB.NetmemberNasenbaaer5 Oct '11 - 3:46 
My piece of code in VB.Net
 
 <Runtime.InteropServices.DllImport("kernel32.dll", CharSet:=Runtime.InteropServices.CharSet.Ansi, SetLastError:=True)> _
            Private Function WritePrivateProfileString(Section As String, Key As String, Value As String, FilePath As String) As Long
            End Function
 
            <Runtime.InteropServices.DllImport("kernel32.dll", CharSet:=Runtime.InteropServices.CharSet.Ansi, SetLastError:=True)> _
            Private Function GetPrivateProfileString(Section As String, Key As String, DefaultValue As String, Result As System.Text.StringBuilder, Size As Integer, FilePath As String) As Long
            End Function
 

            ''' <summary>
            ''' Modify or create an value in an ini file. 
            ''' </summary>
            ''' <param name="FilePath">Path of ini file for example: C\Mydirectory\file.ini</param>
            ''' <param name="Section">Keyword Parent</param>
            ''' <param name="Key">Keyword</param>
            ''' <param name="Value">Value to store</param>
            ''' <returns>True if successfully</returns>
            ''' <remarks>Timo Böhme, www.goldengel.ch, info@goldengel.ch, http://msdn.microsoft.com/en-us/library/windows/desktop/ms724353%28v=vs.85%29.aspx
            ''' IniFileModifyValue:
            ''' If the function successfully copies the string to the initialization file, the return value is nonzero.</remarks>
            Public Function IniFileModifyValue(ByVal FilePath As String, ByVal Section As String, ByVal Key As String, ByVal Value As String) As Boolean
                Dim ret As Long
                Try
                    ret = WritePrivateProfileString(Section, Key, Value, FilePath)
                    If ret = 0 Then Return False
                Catch ex As Exception
                    Return False
                End Try
                Return True
            End Function
 

            ''' <summary>
            ''' Read an existing value in an ini file. Returns the default value if file or key does not exist.
            ''' </summary>
            ''' <param name="FilePath">Path of ini file for example: C\Mydirectory\file.ini</param>
            ''' <param name="Section">Keyword Parent</param>
            ''' <param name="Key">Keyword</param>
            ''' <param name="DefaultValue">Value to store</param>
            ''' <returns>The value from ini file</returns>
            ''' <remarks>http://msdn.microsoft.com/en-us/library/windows/desktop/ms724353%28v=vs.85%29.aspx
            ''' GetPrivateProfileString 
            ''' The return value is the number of characters copied to the buffer, not including the terminating null character.</remarks>
            Public Function IniFileReadSingleValue(FilePath As String, Section As String, Key As String, DefaultValue As String) As String
                Dim ret As Long
                Dim res As String = DefaultValue
                Dim sb As New System.Text.StringBuilder(255)
                If IO.File.Exists(FilePath) = True Then
 

                    ret = GetPrivateProfileString(Section, Key, DefaultValue, sb, sb.Capacity, FilePath)
                    If ret > 0 Then
                        ret = Global.System.Math.Min(ret, sb.Capacity) 'important! because ret could be more than 255
                        ret = Global.System.Math.Min(ret, sb.Length) 'important! because buffer can be unused and less than 255
                        res = sb.ToString(0, ret)
                    End If
                End If
 
                Return res
            End Function

GeneralMy vote of 5memberNasenbaaer5 Oct '11 - 3:45 
Thanks
Questionhave 5 starsmemberShivamkalra22 Jun '11 - 18:57 
It solved my problem and useful article..
Question5 starmemberShivamkalra22 Jun '11 - 18:56 
It solved my problem and useful article..
RantOKmembermebaba12 May '11 - 23:06 
this Cod is perfect .. Thx a lot Smile | :) Smile | :)
GeneralMy vote of 5memberMember 4458079 Jose5 May '11 - 15:35 
Nice job. Thank you.
GeneralMy vote of 5memberdv7617 Mar '11 - 22:20 
Great article
Generalsuggestion to add some more methods to this class.memberhugoandrioli@yahoo.com28 Dec '10 - 0:15 
///
/// see IniWriteValue, but write an integer
///

public void IniWriteInt(string section, string key, int value)
{

String s = Convert.ToString(value);
IniWriteValue(section, key, s);
}
 
///
/// see IniReadValue, but read as an integer.
///

public int IniReadInt(string section, string key)
{
String s= IniReadValue(section,key);
String exceptionMsg = "string could not be converted to an int";
int value;
try
{
value = Convert.ToInt32(s);
}
catch (FormatException ex)
{
throw new IniFileException(exceptionMsg, ex);
}
catch (OverflowException ex)
{
throw new IniFileException(exceptionMsg, ex);
}
return value;
}
 

public class IniFileException : Exception
{
//TODO put this class in it's own file.
 

public IniFileException(String message)
: base(message)
{
}
 
public IniFileException(string message, Exception ex)
: base(message, ex)
{
}
}
GeneralRe: suggestion to add some more methods to this class.membermd5sum5 Jan '11 - 9:28 
This is way overcomplicated. Try this instead:
public class IniFile
{
    public string path;
 
    [DllImport("kernel32")]
    private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
 
    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
 
    public IniFile(string INIPath)
    {
        path = INIPath;
    }
 
    public void IniWriteValue(string Section, string Key, string Value)
    {
        WritePrivateProfileString(Section, Key, Value, this.path);
    }
 
    public string IniReadValue(string Section,string Key, string Def)
    {
        StringBuilder temp = new StringBuilder(255);
        int i = GetPrivateProfileString(Section, Key, (def != null ? def : string.Empty), temp, 255, this.path);
        return temp.ToString();
    }
 
    public void IniWriteValue(string Section, string Key, int Value)
    {
        IniWriteValue(Section, Key, Value.ToString());
    }
 
    public int IniReadValue(string section, string key, int? def)
    {
        string s = ReadValue(section, key, (def != null ? def.ToString() : null));
        int i = 0;
        if (int.TryParse(s, out i))
            return i;
        else
            throw new IniFileException("Tried to retrieve invalid value type from ini.");
    }
}
 
public class IniFileException : Exception
{
    public IniFileException(string Message)
        : base(Message)
    {
 
    }
}
 
Your stack trace will now have everything you need, and you can get rid of the messy try...catch...throw. There's no need to include an InnerException as part of the newly thrown IniFileException, as no other Exception has been generated yet.
 
You have room for a default value now, and you're using overloads instead of multiple alternately named, similar functions. This is a more standardized method for this sort of endeavor.
 
An int.TryParse is just a hair slower than a Convert.ToInt32 but the overhead this method will save you on failure is well worth it. Additionally, it's the preferred method for conversion.
GeneralRe: suggestion to add some more methods to this class.member(^-^)h~ugo5 Jan '11 - 11:45 
Hi md5sum, thx for your insight here!
Allthough the methodes I posted
do what they supposed to, I agree
with overloading, the methods you posted.
I made changes accordingly. However I am not sure
about using tryparse. if reading from ini would fail because
the number is too high to be parsed or there is an invalid
format, then you can't distinqush between those cases from
iniexception anymore.
GeneralRe: suggestion to add some more methods to this class. [modified]membermd5sum5 Jan '11 - 12:02 
I suppose if you need that much granularity in your error reporting, then it would be necessary to report it that way. However, I would probably instead change:
throw new IniFileException("Tried to retrieve invalid value type from ini.");
to:
throw new IniFileException(string.Format("Tried to retrieve invalid value type from ini. Tried to convert \"{0}\" to int.", s));
in order to introduce the same level of reporting the cause of the error without the granularity of exception types generated through the alternate, multiple catch method. I can't imagine an instance where I would need (for myself or my users) to have more than a single exception generated from this block of code. In general though, if you absolutely want to keep the InnerException and specifically NOT use a TryParse, I would limit it to simply:
try
{
    i = Convert.ToInt32(s);
}
catch (Exception ex)
{
    throw new IniFileException("Tried to retrieve invalid value type from ini.", ex);
}
and add the following constructor back to the IniFileException class:
public IniFileException(string Message, Exception ex)
    : base(Message, ex)
{
 
}
 
This will keep your granularity in exception types, rethrowing the wrapped exception with an appropriate InnerException. I just can't imagine why you would want that in this particular case.
modified on Friday, January 7, 2011 2:38 PM

GeneralRe: suggestion to add some more methods to this class.membertttony076 Mar '11 - 15:48 
good class Thumbs Up | :thumbsup:
 
but where is the ReadValue() method??
 
EDIT:
 
is IniReadValue()
 
Thanks for the code!!
GeneralStrings longer than 254 characters will be truncated.memberdgph21 Nov '10 - 17:34 
The code will only be able to retrieve strings that are 254 characters or less (to fit in a null-terminated buffer of 255 characters), otherwise they will be truncated.
int i = GetPrivateProfileString(Section,Key,"",temp, 255, this.path);

GeneralRe: Strings longer than 254 characters will be truncated.memberf.rivato16 Jan '13 - 21:35 
Found the same problem. May be the author should easy fix this bug?
GeneralMy vote of 3memberDaniel Moreland4 Nov '10 - 12:10 
don't explain exactly where tu put the items. nice help, but to those who has some knoledge in C# already
GeneralUnable to add multiple recordmembersammizai4 Oct '10 - 23:58 
The solution work fine with one time input.
But problem come when read/write multiple line to the ini.
Every new record only replace the previous record.
 
Any solution to solve it?
AnswerRe: Unable to add multiple recordmembershrutigupta2124 Dec '10 - 7:49 
CAN BE DONE THROUGH
FILE.Writeallline()
 
and can be read through file.readallline
 

file is beeter than stream read and stream write because stream write is used once if we used more than once it overwrite thats file is beeter option because it does not overwrite and there is no use of close0, flush in file .
GeneralRe: Unable to add multiple recordmemberkytro36028 Oct '12 - 5:53 
Can you elaborate on this?
 
How would we implement File.WriteAllLines with the class?
GeneralGreat Jobmembersos.crow16 Jul '09 - 16:50 
Hey, nice job. I just posted an implementation I wrote a while ago on my site. It doesn't provide an interface to write to the Ini though just read from it. Take a look at it and let me know what you think.
 
http://www.crowsprogramming.com/archives/95
GeneralCode Update!memberBenjamin Ethington16 Jun '09 - 9:31 
I added a IniGetCategories() and IniGetKeys() functions to the class based on a post I read at How to access INI Files in C# .NET - By Gerhard Stephan[^]
 
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections.Generic;
 
namespace Ini
{
	public class IniFile
	{
		public string path;
 
		[DllImport("KERNEL32.DLL",   EntryPoint = "GetPrivateProfileStringW",
          SetLastError=true,
          CharSet=CharSet.Unicode, ExactSpelling=true,
          CallingConvention=CallingConvention.StdCall)]
        private static extern int GetPrivateProfileString(
          string lpAppName,
          string lpKeyName,
          string lpDefault,
          string lpReturnString,
          int nSize,
          string lpFilename);
		[DllImport("KERNEL32.DLL", EntryPoint="WritePrivateProfileStringW",
          SetLastError=true,
          CharSet=CharSet.Unicode, ExactSpelling=true,
          CallingConvention=CallingConvention.StdCall)]
        private static extern int WritePrivateProfileString(
          string lpAppName,
          string lpKeyName,
          string lpString,
          string lpFilename);
 
		public IniFile(string INIPath)
		{
			path = INIPath;
		}
		public void IniWriteValue(string Section,string Key,string Value)
		{
			WritePrivateProfileString(Section,Key,Value,this.path);
		}
		public string IniReadValue(string Section,string Key)
		{
            string result = new string(' ', 255);
            GetPrivateProfileString(Section, Key, "", result, 255, this.path);
            return result;
 
		}
        public List<string> IniGetCategories()
        {
            string returnString = new string(' ', 65536);
            GetPrivateProfileString(null,null,null,returnString,65536,this.path);
            List<string> result = new List<string>(returnString.Split('\0'));
            result.RemoveRange(result.Count - 2, 2);
            return result;
        }
        public List<string> IniGetKeys(string category)
        {
            string returnString = new string(' ', 32768);
            GetPrivateProfileString(category, null, null, returnString, 32768, this.path);
            List<string> result = new List<string>(returnString.Split('\0'));
            result.RemoveRange(result.Count-2,2);
            return result;
        }
	}
}
 
Hope someone finds this useful!
 
Ben Ethington
GeneralRe: Code Update! - ConvertIni2XmlmemberThymine4 Aug '09 - 8:27 
I thought i would write a quick conversion from INI to Xml based on your additions to this class.
Here is what i came up with
 
public static void ConvertIni2Xml(string IniFileName, string XmlOutputFileName)
{
	if (string.IsNullOrEmpty(XmlOutputFileName))
	{
		XmlOutputFileName = Path.Combine(Path.GetDirectoryName(IniFileName), String.Format("{0}.xml", Path.GetFileNameWithoutExtension(IniFileName)));
	}
 
	IniFile iniFile = new IniFile(IniFileName);
	XmlTextWriter xw = null;
 
	try
	{
		xw = new XmlTextWriter(XmlOutputFileName, Encoding.UTF8);
 
		//Start the XmlDocument
		xw.WriteStartDocument();
		xw.WriteStartElement("configuration");
 
		foreach (string categoryName in iniFile.IniGetCategories())
		{
			xw.WriteStartElement("category");
			xw.WriteAttributeString("name", categoryName);
 
			foreach (string keyName in iniFile.IniGetKeys(categoryName))
			{
				//Write the XML Name/Value Node to the xml file
				xw.WriteStartElement("setting");
				xw.WriteAttributeString("name", keyName);
				xw.WriteAttributeString("value", iniFile.IniReadValue(categoryName, keyName));
				xw.WriteEndElement();
			}
 
			xw.WriteEndElement();
		}
 
		xw.WriteEndElement(); //close configuration
		xw.WriteEndDocument();
 
	}
	catch (Exception)
	{
 
		throw;
	}
	finally
	{
		if (xw != null)
		{
			xw.Close();
		}
	}
}

 
modified on Tuesday, August 4, 2009 2:35 PM

GeneralRe: Code Update!memberthund3rstruck18 Sep '09 - 4:46 
Adding to Ben's changes.. here's a method for fetching all the keys and their values together. (e.g. SourceFolder=C:\Windows\Temp, DestinationFolder=E:\Audit\Archives)
 
public List<string> GetKeysAndValues(string section)
{
  List<string> result = new List<string>();
  foreach (string key in GetKeys(section))
  {
     result.Add(key + "=" + ReadValue(section, key));
  }
  return result;
}

GeneralAdded some methods to manage additional data types (read)memberNyarlatotep15 Jun '09 - 23:42 
This is a nice class.
I'm a C# newbie and tried to add some additional methods useful for a project of mine.
 
        public int IniReadValue(string Section, string Key, int DefaultValue)
        {
            string sTemp = IniReadValue(Section, Key, "");
            if (sTemp.Length == 0)
            {
                return DefaultValue;
            }
            return Int32.Parse(sTemp);
        }
 
        public ulong IniReadValue(string Section, string Key, ulong DefaultValue)
        {
            string sTemp = IniReadValue(Section, Key, "");
            if (sTemp.Length == 0)
            {
                return DefaultValue;
            }
            return ulong.Parse(sTemp);
        }
 
        public double IniReadValue(string Section, string Key, double DefaultValue)
        {
            string sTemp = IniReadValue(Section, Key, "");
            if (sTemp.Length == 0)
            {
                return DefaultValue;
            }
            return double.Parse(sTemp, CultureInfo.GetCultureInfo("en-US").NumberFormat);
        }
 
        public bool IniReadValue(string Section, string Key, bool DefaultValue)
        {
            bool RetVal = DefaultValue;
            string sTemp = IniReadValue(Section, Key, "");
            if (sTemp.Length == 0)
            {
                return DefaultValue;
            }
            if (!bool.TryParse(sTemp, out RetVal))
            {
                return (Int32.Parse(sTemp) == 0) ? false : true;
            }
            return RetVal;
        }

GeneralRe: Added some methods to manage additional data types (read)memberAli Fakoor21 Oct '11 - 21:25 
But you defined no method to accept third parameter as string: IniReadValue(Section, Key, "")
GeneralRe: Added some methods to manage additional data types (read)memberNyarlatotep21 Oct '11 - 21:40 
It could be accomplished by modifying the original IniReadValue() method and adding a third parameter to provide a default string to be used when the searched key cannot be found.
GeneralUsing this class in a aspx web applicationmemberScott Reid4 Mar '09 - 12:47 
I have been using this .ini class for a while in my test apps that I am developing in C#. Recently I have been building web applications using Visual Studio 2008. I have this .ini class imported into my project and it all seems to work just fine when I test the app on my computer(The one that I developed the app on) but when i publish it to the remote web server, the class does not seem to work, it does not even give me an error. are there any changes i need to make to get it to read and write an .ini file on the server that is hosting the applicaiton?
GeneralHits the spotmemberHibbertM13 Feb '09 - 0:23 
Excellent code, exactly what I needed ... thanks.
 
regards
 
Mark Hibbert

GeneralThanksmemberNAMhatre18 Sep '08 - 7:47 
It really made my job easy.. Really appriciated!!
 
Clean n Neat code..!! Thanks!!
 
Niks

Questionlicensing?memberNick Trown14 Aug '08 - 10:47 
What are the licensing terms for this? Can I free redistribute this code as part of my products?
Generala questionmembergyue31 Jul '08 - 21:18 
why is the path public not private?
GeneralBad pathmemberarj200018 Jul '08 - 3:32 
Without full path, this don't create any file:
INIFile ini = new INIFile("test.ini");
This is the solution:
INIFile ini = new INIFile(".\\test.ini");

GeneralRe: Bad pathmemberBatzen17 Feb '09 - 1:11 
That's not completly correct.
The default path for the file is %WINDIR% (in my case C:\WINDOWS).
GeneralRe: Bad pathmemberRalala Yves3 Jan '10 - 18:32 
How to put it in the application folder?
INIFile ini = new INIFile(".\\test.ini");
doesn't work
GeneralNo Using API Windows (Kernel 32)membercolijunior8 Jul '08 - 10:56 
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using System.Collections.Specialized;
 
namespace Utils
{
 
public class INIFileHandler
{
private Sections m_sections = new Sections();
private char[] vbCrLf = new char[] { '\n', '\r' };
 
public INIFileHandler() { }
 
public INIFileHandler(string strINIFile)
{
ReadFile(strINIFile);
}
 
public void Dispose()
{
if (m_sections != null)
m_sections = null;
}
 
public Sections Sections()
{
return m_sections;
}
 
public Section Sections(string name)
{
Section secReturn = new Section("");
 
try
{
secReturn = m_sections.Section(name);
}
catch (Exception ex)
{ }
 
if (secReturn != null)
return secReturn;
else
return new Section("");
}
 
public Section Sections(int index)
{
Section secReturn = new Section("");
 
try
{
secReturn = m_sections.Section(index);
}
catch (Exception ex)
{
}
 
if (secReturn != null)
return secReturn;
else
return new Section("");
 
}
 
public bool ReadFile(string strINIFile)
{
 
bool blnOK = false;
 
try
{
StreamReader srFile = File.OpenText(strINIFile);
string strFile = srFile.ReadToEnd();
 
string strSectionName = "";
string[] strSetting = null;
string strKey = "";
string strValue = "";
string[] strLine = strFile.Split(vbCrLf);
 
//~~~~~~~~~~~~~~~~~~~~~~~~
//~ Zap files
//~~~~~~~~~~~~~~~~~~~~
strFile = null;
srFile.Close();
srFile = null;
 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~ Loop for each line in file
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for (int nI = 0; nI < strLine.Length; nI++)
{
if ((strLine[nI].Trim().Length > 0) && (!strLine[nI].StartsWith(";")))
{
if (strLine[nI].ToUpper().StartsWith("["))
{
strSectionName = strLine[nI].Substring(1, strLine[nI].Length - 2);
m_sections.AddSection(strSectionName);
}
else if (strSectionName.Length > 0)
{
strSetting = strLine[nI].Split('=');
strKey = strSetting[0].ToString().Trim();
strValue = strSetting[1].ToString().Trim();
m_sections.AddSetting(strKey, strValue);
}
}
}
 
blnOK = true;
}
catch (Exception ex)
{
blnOK = false;
}
 
return blnOK;
}
 
public bool WriteINIFile(string strFileName)
{
 
bool blnOK = false;
 
try
{
string strINIDoc = this.ToString();
 
if (strINIDoc != null && strINIDoc.Length > 0)
{
FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write);
StreamWriter writer = new StreamWriter(fs);
writer.Write(strINIDoc);
writer.Flush();
fs.Flush();
fs.Close();
fs = null;
 
blnOK = true;
}
else
{
blnOK = false;
}
}
catch (Exception ex)
{
blnOK = false;
}
 
return blnOK;
 
}
 
public override string ToString()
{
 
string strReturn = "";
 
try
{
string strINIDoc = "";
 
for (int nSec = 0; nSec < m_sections.Count; nSec++)
{
strINIDoc += "[" + m_sections.Section(nSec).Name + "]" + vbCrLf;
for (int nSetting = 0; nSetting < m_sections.Section(nSec).Settings.Count; nSetting++)
{
string strSetting = m_sections.Section(nSec).Settings[nSetting];
string strKey = m_sections.Section(nSec).Keys(nSetting);
strINIDoc += strKey + "=" + strSetting + vbCrLf;
}
strINIDoc += "" + vbCrLf;
}
 
strReturn = strINIDoc.Trim();
}
catch (Exception ex)
{
strReturn = "";
}
 
return strReturn;
 
}
}
 
public class Sections : System.Collections.Specialized.NameObjectCollectionBase
{
private Section m_section;
public Sections()
{
}
 
public void AddSection(string name)
{
m_section = new Section(name);
base.BaseAdd(name, m_section);
}
 
public void AddSetting(string name, string value)
{
m_section.AddSetting(name, value);
}
 
public Section Section(string name)
{
return (Section)base.BaseGet(name);
}
 
public Section Section(int index)
{
return (Section)base.BaseGet(index);
}
}
 
public class Section
{
private NameValueCollection m_colSettings = new NameValueCollection();
private string m_strSectionName = "";
 
public Section(string name)
{
m_strSectionName = name;
}
 
public void Dispose()
{
if (m_colSettings != null)
{
m_colSettings = null;
}
}
 
public String Name
{
get
{
return m_strSectionName;
}
}
 
public void AddSetting(string name, string value)
{
m_colSettings.Add(name, value);
}
 
public System.Collections.Specialized.NameValueCollection Settings
{
get
{
return m_colSettings;
}
}
 
public string Setting(string name)
{
string strReturn = "";
 
try
{
strReturn = m_colSettings[name];
}
catch (Exception ex)
{
strReturn = "";
}
 
if (strReturn != null)
{
return strReturn;
}
else
{
return "";
}
}
 
public string Setting(int index)
{
string strReturn = "";
 
try
{
strReturn = m_colSettings[index];
}
catch (Exception ex)
{
strReturn = "";
}
 
return strReturn;
}
 
public string Keys(int index)
{
string strReturn = "";
 
try
{
strReturn = m_colSettings.GetKey(index);
}
catch (Exception ex)
{
strReturn = "";
}
 
if (strReturn != null)
{
return strReturn;
}
else
return "";
}
}
}
Generalexceptionmemberfangar13 May '08 - 4:45 
Could anyone tell me which are the exception i need to check in order to avoid any crash of the application when i use dll of the kernel ??
 
Thanks a lot
GeneralThanksmemberAsmor22 Apr '08 - 9:23 
I've been using your code for a while in several different projects and I just realized I never thanked you for it. So... thanks!
GeneralCheck out SnapConfigmemberMember 30271103 Jan '08 - 8:28 
at http://www.snapconfig.com
 
if you are working with ini files its guaranteed to make your life easier.
 

 
None

GeneralA small item to add in...memberreo6328 Sep '07 - 10:52 
I had no problem getting this to work.
Very well done!
 
One thing I did find I needed was a way to pass a default string if the INI is empty or missing
the keyword.
 
This was a cinch...
 
public string IniReadValue(string sSection, string sKey, string sDef)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString ( sSection, sKey, "", temp, 255, this.path );
string sTemp = temp.ToString();
//
// If empty, put in the supplied default
if (sTemp == string.Empty)
sTemp = sDef;
//
// Return the string
return sTemp;
}
GeneralRe: A small item to add in...memberMember 392764214 Aug '09 - 12:21 
The third argument to GetPrivateProfileString is the default. Just pass sDef in place of "".
 
public string IniReadValue(string sSection, string sKey, string sDef)
{
  StringBuilder temp = new StringBuilder(255);
  int i = GetPrivateProfileString ( sSection, sKey, sDef, temp, 255, this.path );
  return temp.ToString();
}

GeneralGood, not so clear.memberTechieFreak17 Oct '07 - 22:36 
Where is the file created??????!??!?? Hmmm | :|

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 15 Mar 2002
Article Copyright 2002 by BLaZiNiX
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid