
.NET 1.1+Reading and Writing INI Files (2)
Initialisation files known as INI files provide a standard means for storing configuration information for software in a text file. Although rarely used by .NET applications, there are situations where these files must be read and written to using C#.
ReadPrivateProfileString
ReadPrivateProfileString is used to read data from an INI file. Depending upon its use, the function reads single values, all key names from a section or all section names:
uint GetPrivateProfileString(
string lpAppName, string lpKeyName, string lpDefault, string lpReturnedString,
uint nSize, string lpFileName);
The function returns an unsigned integer that contains the number of characters in the returned value. The parameters are as follows:
- lpAppName. Specifies the name of the section that is to be read. This can be set to null to return all of the section names.
- lpKeyName. Specifies the name of the key that is to be read. If set to null when lpAppName is not null, the returned value will contain the names of the keys in the specified section.
- lpDefault. Specifies a default value to return if the key is not present.
- lpReturnedString. This parameter is provided with a string that acts as a buffer. The result of the operation is written into the buffer, changing the value of the string.
- nSize. Specifies the maximum size of the string that will be returned in lpReturnedString. This should be equal to the size of the buffer. The value read from the INI file will be truncated if it is longer than nSize.
- lpFileName. Specifies the path and name of the INI file to be read. This file must exist.
NativeMethods Class
Now that we've seen the signatures of the Windows API functions that we'll be using, let's create a class to hold them. Create a new class named, "NativeMethods" and add the following using directive to give easy access to the P/Invoke attributes.
using System.Runtime.InteropServices;
The code for the class is as follows. Note that no access modifier has been included for the class itself so it will have an internal scope. This will allow the IniFile class to use the API functions without exposing them publicly.
class NativeMethods
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool WritePrivateProfileString(
string lpAppName, string lpKeyName, string lpString, string lpFileName);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern uint GetPrivateProfileString(
string lpAppName, string lpKeyName, string lpDefault, string lpReturnedString,
uint nSize, string lpFileName);
}
IniFile Class
We can now create the IniFile class, which will provide simple access to INI files without knowledge of the API functions. Add a new class to the project, naming it "IniFile", and add the following using directives. These will be used to handle exceptions from the API functions.
using System.ComponentModel;
using System.Runtime.InteropServices;
The IniFile class should be made public.
8 December 2011