Mkulu Ini File Manager

Audience: Developers
Operating System: Windows 32 and 64 bit
Tested Under: Windows XP, Windows Vista, Windows 7
Development Language: C#
.NET Version: 3.5
License: zlib license
Latest Release: 1.0

Description

I am obviously a throwback, but I hate using the registry for simple configuration files, and the new XML based approach promoted by .NET is nothing short of nightmarish. So I wrote this simple wrapper class to let me use good old fashioned INI files. One day, if I'm not feeling too lazy and Microsoft ever deprecates the INI API I may rewrite this as pure C#. Until then, here it is.

Details

The public interface is very simple and should be familiar to anyone who's worked with INI API's before

IniFileManager(string fileToManage)
string getString(string section, string key, string defaultValue)
int getInt(string section, string key, int defaultValue)
bool getBool(string section, string key, bool defaultValue)
void writeString(string section, string key, string value)
void writeInt(string section, string key, int value)
void writeBool(string section, string key, bool value)
List getSections()
StringDictionary getSection(string section)

Her's an example from the test bed showing how the IniFileManager is used

private void IniFileManagerTest() {
   Stopwatch sw = new Stopwatch();
   sw.Start();
   IniFileManager ini = new IniFileManager(System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\testfile.ini");
   sw.Stop();
   tbLog.AppendText("IniFileManager init took " + sw.Elapsed.TotalMilliseconds.ToString() + Environment.NewLine);

   string buffer = "";

   sw.Reset();
   sw.Start();
   for (int i = 0; i < 10000; i++) {
      buffer = ini.getString("Production", "ServerName", "");
   }
   sw.Stop();
   tbLog.AppendText("1000 cached reads in " + sw.Elapsed.TotalMilliseconds.ToString() + Environment.NewLine);

   tbLog.AppendText("getString Production\", \"ServerName\", \" = " + buffer + Environment.NewLine);

   buffer = ini.getString("Productions", "ServerName", "");
   tbLog.AppendText("getString Productions\", \"ServerName\", \" = " + buffer + Environment.NewLine);

   buffer = ini.getString("Production", "ServerNames", "");
   tbLog.AppendText("getString Production\", \"ServerNames\", \" = " + buffer + Environment.NewLine);

   bool foo = ini.getBool("Production", "ServerName", false);
   tbLog.AppendText("getBool Production\", \"ServerNames\", false = " + foo.ToString() + Environment.NewLine);

   int bar = ini.getInt("Production", "ServerName", 99);
   tbLog.AppendText("getInt Production\", \"ServerName\", 99 = " + bar.ToString() + Environment.NewLine);

   bar = ini.getInt("Production", "ServerPort", 99);
   tbLog.AppendText("getInt Production\", \"ServerPort\", 99 = " + bar.ToString() + Environment.NewLine);

   ini.writeString("TEST", "string", "value");
   ini.writeInt("TEST", "int", 12984);
   ini.writeBool("TEST", "bool", true);

   List sections = ini.getSections();

   tbLog.AppendText("Listing all sections (w. contents):" + Environment.NewLine);

   foreach (string section in sections) {
      tbLog.AppendText("  [" + section + "]" + Environment.NewLine);

      StringDictionary contents = ini.getSection(section);
      foreach (DictionaryEntry entry in contents) {
         tbLog.AppendText("    " + entry.Key + "=" + entry.Value + Environment.NewLine);
      }
   }

}

Download and Support Information

You will find all the downloads at
http://sourceforge.net/projects/mkuluutils/files/IniFileManager/

If you want to check out the latest Mkulu Utils source from Subversion, the URL is
https://mkuluutils.svn.sourceforge.net/svnroot/mkuluutils/Trunk/

If you just want to browse the source code in Subversion, you can do so at this link
http://mkuluutils.svn.sourceforge.net/viewvc/mkuluutils/Trunk/

For support or to create a bug report, please use the tools at
http://sourceforge.net/projects/mkuluutils/support


File inifilemanager.shtml last updated Thursday, 24-Feb-2011 12:54:09 PST