Reading Configuration Files Without Using Win32

A C# Configuration File Reader that doesn't use the Win32 API functions.

.

Environment: .Net, C#

This is just a small class to handle configuration files. There are several other classes like this one, but they all use the GetPrivateProfileString()/SetPrivateProfileString() family of functions from the Win32 API.

If you check the documentation for those API, you'll notice that they are provided merely for compatability with Win16 applications. It's been quite some time since anyone wrote Win16 applications. (At least, I hope so.) You can find a C# class that uses those API here, using Interop. I don't like to use compatability functions; Microsoft is usually good with compatability, but still... and when you add Interop to the mess, I decided to do it on my own.

So I wrote a class that does it for me, without using Interop. It uses the same syntax as all INI files for Windows, and it supports sections and section-less keys.

Configuration conf =
 new Configuration(new System.IO.StreamReader("Configuration.ini"));
string UserName = conf.GetValue("User");
string HomePage = conf.GetValue("Internet_Data","Home_Page");

conf.SetValue( "User_Information", 
               "Log_In_Time",
               System.DateTime.Now.ToString());

Pretty simple, isn't it? Now, it's not XML, and it doesn't have hype all over it, but it's small, efficent, and allows data hiding. If you want to switch the back end from an INI file to xn XML file, it's just a matter of changing the Parse() & Save() methods. The code is well commented, you shouldn't have any trouble with it. If you do have problems, I'll be glad to hear about them.

Downloads

Download demo project - 5.67 Kb
Download source - 3.61 Kb



Comments

  • Slick and Simple

    Posted by callum_bell on 03/04/2004 07:22pm

    Very nice, just what I needed.

    Reply
  • remove functions

    Posted by Legacy on 02/21/2003 12:00am

    Originally posted by: Lee

    how about this:
    
    

    public void RemoveValue(string Key, string ValueName)
    {
    System.Collections.Hashtable KeyTable = (System.Collections.Hashtable)Keys[Key];
    if(KeyTable!=null)
    KeyTable.Remove(ValueName);
    else
    throw new Exceptions.ConfigurationKeyDoesntExist();
    }

    public void RemoveValue(string ValueName)
    {
    Values.Remove(ValueName);
    }

    public void RemoveKey(string Key)
    {
    System.Collections.Hashtable KeyTable = (System.Collections.Hashtable)Keys[Key];
    if(KeyTable!=null)
    Keys.Remove(Key);
    else
    throw new Exceptions.KeyDoesntExistException();
    }


    hope it helps,

    - Lee

    Reply
  • Nice Function

    Posted by Legacy on 11/20/2002 12:00am

    Originally posted by: Robert

    Worked well for me..

    Reply
  • Delete Keys

    Posted by Legacy on 10/12/2002 12:00am

    Originally posted by: DB

    It would be nice if there was a delete function for keys and name pair values.

    Kinda like:

    void SetValue(string Key, string ValueName, string Value)

    But like:
    void RemoveValue(string Key)

    Where if only the key was provided then the entire key would be removed including ValueNames and it's Values.

    Where:
    void RemoveValue(string Key, string ValueName)

    If the ValueName was provided with the key then just the ValueName would be removed within the key.

    Reply
  • Log File System for multithreaded application

    Posted by Legacy on 07/12/2002 12:00am

    Originally posted by: Ram Sharma

    Does anyone know how to write to a log file but make the 'write' thread safe? I am not sure how to synchronize the writing to log file from various threads.

    Thanks
    Ram

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Businesses are moving more and more of their customer transactions to the web. Security is understandably a top concern as online transactions increase, so it is important to make sure your electronic signature provider meets the highest security standards. That means more than simply passing a security audit or obtaining a certification. This white paper provides recommendations for taking a broader view of e-signature security, and answers key questions that help identify the security requirements against …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds