Files
1440WGJ/SimpleServer/IniHelper.cs
T
2026-07-14 13:55:17 +08:00

60 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace SimpleCommunication
{
public class IniHelper
{
public string path;
public IniHelper(string INIPath)
{
path = INIPath;
}
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern int GetPrivateProfileString(string section, string key, string defVal, byte[] retVal, int size, string filePath);
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, path);
}
public string IniReadValue(string Section, string Key)
{
try
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
return temp.ToString();
}
catch (Exception)
{
return null;
}
}
public byte[] IniReadValues(string section, string key)
{
byte[] temp = new byte[255];
int i = GetPrivateProfileString(section, key, "", temp, 255, path);
return temp;
}
public void ClearAllSection()
{
IniWriteValue(null, null, null);
}
public void ClearSection(string Section)
{
IniWriteValue(Section, null, null);
}
}
}