using System.Runtime.InteropServices;
using System.Text;
namespace FileOperate;
public class INI
{
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string Section, string KeyName, string KeyValue, string FilePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string Section, string KeyName, string ValueDefult, StringBuilder KeyValue, int Size, string FilePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string Section, string KeyName, string ValueDefult, byte[] KeyValue, int Size, string FilePath);
public long IniWrite(string Section, string KeyName, string KeyValue, string FilePath)
{
return WritePrivateProfileString(Section, KeyName, KeyValue, FilePath);
}
public int IniRead(string Section, string KeyName, StringBuilder KeyValue, string FilePath)
{
return GetPrivateProfileString(Section, KeyName, "defult", KeyValue, 2048, FilePath);
}
public string[] IniReadKey(string Section, string FilePath)
{
byte[] array = new byte[2048];
GetPrivateProfileString(Section, null, "defult", array, 2048, FilePath);
string @string = Encoding.UTF8.GetString(array);
char[] trimChars = new char[1];
string text = @string.Trim(trimChars);
trimChars = new char[1];
return text.Split(trimChars);
}
public string[] IniReadSection(string FilePath)
{
byte[] array = new byte[2048];
GetPrivateProfileString(null, null, "defult", array, 2048, FilePath);
string @string = Encoding.UTF8.GetString(array);
char[] trimChars = new char[1];
string text = @string.Trim(trimChars);
trimChars = new char[1];
return text.Split(trimChars);
}
}
调用:
string path = AppDomain.CurrentDomain.BaseDirectory + "Config.ini";
if (!File.Exists(text))
{
throw new Exception("找不到Config文件" + text);
}
INI iNI = new INI();
StringBuilder stringBuilder1 = new StringBuilder();
iNI.IniRead("Config", "IsPermit", stringBuilder1, path);