c# How to work with ini files

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace sIniFile
{
    /// <summary>
    /// Descrizione di riepilogo per IniFileClass.
    /// </summary>
    public class IniFileClass
    {
        private String IniFile = "";
        
        // Importazione dei metodi dalle DLL di sistema
        [DllImport("kernel32")] private static extern long WritePrivateProfileInt(String Section, String Key, int Value, String FilePath);
        [DllImport("kernel32")] private static extern long WritePrivateProfileString(String Section, String Key, String Value, String FilePath);
        [DllImport("kernel32")] private static extern int GetPrivateProfileInt(String Section, String Key, int Default, String FilePath);
        [DllImport("kernel32")] private static extern int GetPrivateProfileString(String Section, String Key, String Default, StringBuilder retVal, int Size, String FilePath);

        public IniFileClass(String IniFile)
        {
            // Mi salvo il percorso del file INI
            this.IniFile = IniFile;
        }

        public String ReadString(String Section, String Key, String Default)
        {
            // Creo lo StringBuilder che conterra la stringa
            StringBuilder StrBu = new StringBuilder(255);
            // Provo a leggere il parametro richiesto
            GetPrivateProfileString(Section, Key, Default, StrBu, 255, IniFile);
            // Restituisco il parametro letto
            return StrBu.ToString();
        }

        public int ReadInt(String Section, String Key, int Default)
        {
            // Restituisco il valore del parametro letto
            return GetPrivateProfileInt(Section, Key, Default, IniFile); 
        }

        public void WriteString(String Section, String Key, String Value)
        {
            // Salvo il valore del parametro impostato
            WritePrivateProfileString(Section, Key, Value, IniFile);
        }

        public void WriteInt(String Section, String Key, int Value)
        {
            // Salvo il valore del parametro impostato
            WritePrivateProfileInt(Section, Key, Value, IniFile);
        }
    }
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.