How to access INI Files in C# .NET

Sometimes you’ll find your self in a situation where you need to use outdated techniques in order to support outdated applications … That time you’ll recognize that .NET does not support INI Files anymore.

So let us take a time machine and write our own INI File access.

First of all you have to declare two methods which are necessary to access an INI File.

[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);

Now let us code a small example that reads a simple ini file.

/*
* Try to read the content of a simle INI file
*/
List<string> categories = GetCategories(iniFile);
foreach (string category in categories)
{
    Console.WriteLine(category);
    
    /*
     * Get the keys
     */
    List<string> keys = GetKeys(iniFile, category);
    foreach (string key in keys)
    {
        /*
         * Now output the content
         */
        string content = GetIniFileString(iniFile, category, key, defaultValue);
        Console.WriteLine(string.Concat(" ", key, "\t", content));
    }
}

As you can see I used three new methods GetCategories, GetKeys and GetIniFileString. These new methods are not an integrated part of the .NET framework. So we have to implement them by our own.

The following method reads a list with all categories the Ini File contains. A category within the Ini File is tagged with brackets, like [ODBC] or [GLOBALS]. The API GetPriveProfileString fills an internal buffer separated by \ 0. In order to get a generic list we have to split that buffer with the token \ 0. The last 2 values can be cutted, because they are always empty.

private static List<string> GetCategories(string iniFile)
{
    string returnString = new string(' ', 65536);
    GetPrivateProfileString(null, null, null, returnString, 65536, iniFile);
    List<string> result = new List<string>(returnString.Split('\ 0'));
    result.RemoveRange(result.Count - 2, 2);
    return result;
}

Using the same technique we can read the keys from a category.

private static List<string> GetKeys(string iniFile, string category)
{
    string returnString = new string(' ', 32768);
    GetPrivateProfileString(category, null, null, returnString, 32768, iniFile);
    List<string> result = new List<string>(returnString.Split('\ 0'));
    result.RemoveRange(result.Count-2,2);
    return result;
}

Lat but not least we need to read the value of the key we found.

private static string GetIniFileString(string iniFile, string category, string key, string defaultValue)
{
    string returnString = new string(' ', 1024);
    GetPrivateProfileString(category, key, defaultValue, returnString, 1024, iniFile);
    return returnString.Split('\ 0')[0];
}

That’s all – and uhh, before I forget. Here’s an example for writing values to an INI File. We can use the declared and imported API method itself, without writing a wrapper to use it.

WritePrivateProfileString("GLOBAL", "Stuff", "The content of that stuff", "MyIniFile.ini");

But be aware, if you do not specify a directory, the INI File resides always within the Windows Directory. Most times this will be C:\Windows\

So have a nice day
Cheers

Gerhard

You can download the example using the following link: How to access INI Files in C# .NET