Katteker/AppStubEx/IniFile.cs
Holger Boerchers d383f0ef1c Initial commit
2018-03-21 20:07:40 +01:00

50 lines
1.6 KiB
C#

using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
namespace Katteker.AppStub
{
public class IniFile // revision 11
{
private readonly FileInfo _path;
private readonly string _exe = Assembly.GetExecutingAssembly().GetName().Name;
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern long WritePrivateProfileString(string section, string key, string value, string filePath);
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern int GetPrivateProfileString(string section, string key, string Default, StringBuilder retVal, int size, string filePath);
public IniFile(string iniPath = null)
{
_path = new FileInfo(iniPath ?? _exe + ".ini");
}
public string Read(string key, string section = null)
{
var retVal = new StringBuilder(255);
GetPrivateProfileString(section ?? _exe, key, "", retVal, 255, _path.FullName);
return retVal.ToString();
}
public void Write(string key, string value, string section = null)
{
WritePrivateProfileString(section ?? _exe, key, value, _path.FullName);
}
public void DeleteKey(string key, string section = null)
{
Write(key, null, section ?? _exe);
}
public void DeleteSection(string section = null)
{
Write(null, null, section ?? _exe);
}
public bool Exists => _path.Exists;
public bool KeyExists(string key, string section = null) => Read(key, section).Length > 0;
}
}