using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace WinISP
{
    
    public class AccessIni
    {
        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        public static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        public static bool IsShowErrorMsg = true;

        public AccessIni() { }

        public static StringBuilder GetProfileString(string section, string key, string filePath)
        {
            StringBuilder retStr = new StringBuilder(255);
            try
            {
                int retError = GetPrivateProfileString(section, key, "", retStr, retStr.Capacity, filePath);

                if (retError == 0)
                {
                    if (IsShowErrorMsg)
                        System.Windows.Forms.MessageBox.Show("section = [ " + section.ToString() + " ] key = [ " + key.ToString() + " ] \n format error !!");
                }
            }
            catch (Exception ex)
            {
                if (IsShowErrorMsg)
                    System.Windows.Forms.MessageBox.Show("section = [ " + section.ToString() + " ] key = [ " + key.ToString() + " ] \n" + ex.Message);
            }
            return retStr;
        }

    }
}
