using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespaceWPF.Common.Helpers
{
publicclassIniFile
{
string _path;
readonlystring _exe = Path.Combine(AppPaths.ExeFolder, AppPaths.ExeName);
[DllImport("kernel32")]
staticexternlongWritePrivateProfileString(string section, string key, stringvalue, string filePath);
[DllImport("kernel32")]
staticexternintGetPrivateProfileString(string section, string key, string Default, StringBuilder retVal, int size, string filePath);
[DllImport("kernel32")]
staticexternintGetPrivateProfileString(string section, int key, stringvalue, [MarshalAs(UnmanagedType.LPArray)] byte[] result, int size, string fileName);
publicIniFile(string iniPath = null)
{
_path = new FileInfo(iniPath ?? _exe + ".ini").FullName;
}
publicstringRead(string key, string section = null)
{
var retVal = new StringBuilder(255);
GetPrivateProfileString(section ?? _exe, key, "", retVal, 255, _path);
return retVal.ToString();
}
publicvoidWrite(string key, stringvalue, string section = null)
{
WritePrivateProfileString(section ?? _exe, key, value, _path);
}
publicboolKeyExists(string key, string section = null)
{
return Read(key, section).Length > 0;
}
public Dictionary<string, string> GetValueDictionary(int position, string section, bool isNumber)
{
var rezult = new Dictionary<string, string>();
var keys = GetSectionKeys(section);
double doubVal;
int intVal;
foreach (var key in keys)
{
varvalue = Read(key, section).Split(new[] {"###"}, StringSplitOptions.None);
if (value.Length >= position &&
(!isNumber || (double.TryParse(value[position], NumberStyles.Any, CultureInfo.InvariantCulture, out doubVal) || (int.TryParse(value[position], out intVal)))))
rezult.Add(key, value[position]);
}
return rezult;
}
publicstring[] GetSectionKeys(string section)
{
// Sets the maxsize buffer to 500, if the more// is required then doubles the size each time. for (int maxsize = 500;; maxsize *= 2)
{
// Obtains the EntryKey information in bytes// and stores them in the maxsize buffer (Bytes array).// Note that the SectionHeader value has been passed.byte[] bytes = newbyte[maxsize];
int size = GetPrivateProfileString(section, 0, "", bytes, maxsize, _path);
// Check the information obtained is not bigger// than the allocated maxsize buffer - 2 bytes.// if it is, then skip over the next section// so that the maxsize buffer can be doubled.if (size < maxsize - 2)
{
// Converts the bytes value into an ASCII char.// This is one long string.string entries = Encoding.GetEncoding("Windows-1251").GetString(bytes, 0,
size - (size > 0 ? 1 : 0));
// Splits the Long string into an array based on the "\0"// or null (Newline) value and returns the value(s) in an arrayreturn entries.Split('\0');
}
}
}
}
}