C# WinAPI: Trabajando con ficheros INI

csharpthebasics[1]¿Cómo trabajar con los ficheros INI usando el .NET Framework?. Los ficheros INI son ficheros de configuración utilizados por las aplicaciones de los sistemas operativos Windows (El término proviene de "Windows Initialization file"). Hoy en día es más común usar el Registro del sistema operativo o almacenar las configuraciones en ficheros XML. Sin embargo muchas características de aplicaciones Windows siguen usando los ficheros INI  (por ejemplo los favoritos del Internet Explorer).

Para crear un programa que lea y escriba ficheros INI desde .NET necesitamos dos métodos definidos en el Kernel32: WritePrivateProfileString  para escribir y GetPrivateProfileString para leer:

[DllImport("kernel32.dll")]
private static extern long WritePrivateProfileString(
   string section,
   string key,
   string val,
   string filePath);

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