using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ConfMan.IOB.Core
{
///
/// Create a new INI file to store or load data
///
public class IniFile
{
#region Public Fields
public string FileName;
#endregion Public Fields
#region Public Constructors
///
/// Constructor
///
///
public IniFile(string INIPath)
{
FileName = INIPath;
}
#endregion Public Constructors
#region Public Methods
///
/// Delete a key from section
///
///
///
public void IniDeleteKey(string Section, string Key)
{
WritePrivateProfileString(Section, Key, null, FileName);
}
///
/// Completely remove one section
///
///
public void IniDeleteSection(string Section)
{
WritePrivateProfileSection(Section, null, FileName);
}
///
/// Return true if section exists
///
///
///
public bool IniSectionExists(string Section)
{
int bytesReturned = 0;
const int bufferSize = 2048; // max is 32767
IntPtr pReturnedString = Marshal.AllocCoTaskMem(bufferSize);
try
{
bytesReturned = GetPrivateProfileSection(Section, pReturnedString, bufferSize, FileName);
}
finally
{
Marshal.FreeCoTaskMem(pReturnedString);
}
return (bytesReturned > 0);
}
///
/// Read a boolean
///
///
///
///
public bool ReadBoolean(string Section, string Key)
{
return (ReadInteger(Section, Key, 0) != 0);
}
///
/// Read a boolean with default value
///
///
///
///
///
public bool ReadBoolean(string Section, string Key, bool DefaultVal)
{
int v = DefaultVal ? 1 : 0;
return (ReadInteger(Section, Key, v) != 0);
}
///
/// Read an integer
///
///
///
///
public int ReadInteger(string Section, string Key)
{
return GetPrivateProfileInt(Section, Key, 0, FileName);
//System.Convert.ToInt32(IniReadValue(Section, Key));
}
///
/// Read an integer. If not found use default value
///
///
///
///
///
public int ReadInteger(string Section, string Key, int DefaultVal)
{
//int temp = System.Convert.ToInt32(IniReadString(Section, Key, Convert.ToString(DefaultVal)));
//return temp;
return GetPrivateProfileInt(Section, Key, DefaultVal, FileName);
}
///
/// Read a complete section (keys=values)
///
///
/// Section name
///
/// restituisce delle stringhe keys=values da suddividere appunto come key/val successivamente
///
public string[] ReadSection(string Section)
{
const int bufferSize = 2048; // max is 32767
StringBuilder returnedString = new StringBuilder();
IntPtr pReturnedString = Marshal.AllocCoTaskMem(bufferSize);
try
{
int bytesReturned = GetPrivateProfileSection(Section, pReturnedString, bufferSize, FileName);
if (bytesReturned > 0)
{
//bytesReturned -1 to remove trailing \0
for (int i = 0; i < bytesReturned - 1; i++)
{
var currPointer = IntPtr.Add(pReturnedString, i);
var currChar = (char)Marshal.ReadByte(currPointer);
returnedString.Append(currChar);
}
}
}
finally
{
Marshal.FreeCoTaskMem(pReturnedString);
}
string sectionData = returnedString.ToString();
return sectionData.Split('\0');
}
///
/// Read data from the Ini file
///
///
///
///
///
public string ReadString(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, FileName);
return temp.ToString();
}
///
/// Read a string. If not found use default value
///
///
///
///
///
public string ReadString(string Section, string Key, string DefaultVal)
{
string temp = ReadString(Section, Key);
if (temp == "") temp = DefaultVal;
return temp;
}
///
/// Return true if value exists
///
///
///
///
public bool ValueExists(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, FileName);
return (i > 0);
}
///
/// Write a boolean value
///
///
///
///
public void WriteBoolean(string Section, string Key, bool Value)
{
int flag = Value ? 1 : 0;
WriteString(Section, Key, Convert.ToString(flag));
}
///
/// Write a double value
///
///
///
///
public void WriteDouble(string Section, string Key, double Value)
{
WriteString(Section, Key, Convert.ToString(Value, NumberFormatInfo.InvariantInfo));
}
///
/// Write an integer value
///
///
///
///
public void WriteInteger(string Section, string Key, int Value)
{
WriteString(Section, Key, Convert.ToString(Value));
}
///
/// Write data to the INI file
///
///
/// Section name
///
/// Key Name
///
/// Value Name
public void WriteString(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, FileName);
}
#endregion Public Methods
#region Private Methods
///
/// GetPrivateProfileInt: import windows dll functions
///
///
///
///
///
///
[DllImport("kernel32")]
private static extern int GetPrivateProfileInt(string section, string key, int def, string filePath);
///
/// GetPrivateProfileSection: import windows dll functions
///
///
///
///
///
///
[DllImport("kernel32")]
private static extern int GetPrivateProfileSection(string section, IntPtr retVal, uint size, string filePath);
///
/// GetPrivateProfileString: import windows dll functions
///
///
///
///
///
///
///
///
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
///
/// WritePrivateProfileSection: import windows dll functions
///
///
///
///
///
[DllImport("kernel32")]
private static extern bool WritePrivateProfileSection(string section, string value, string filePath);
///
/// WritePrivateProfileString: import windows dll functions
///
///
///
///
///
///
[DllImport("kernel32", CharSet = CharSet.Auto, BestFitMapping = false)]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
#endregion Private Methods
}
}