using System; using System.Runtime.InteropServices; using System.Text; namespace ErpServer { public class INI { /// /// 获取所有节点名称(Section) /// /// 存放节点名称的内存地址,每个节点之间用\0分隔 /// 内存大小(characters) /// Ini文件 /// 内容的实际长度,为0表示没有内容,为nSize-2表示内存大小不够 [DllImport("kernel32.dll", CharSet = CharSet.Auto)] private static extern uint GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, uint nSize, string lpFileName); /// /// 写入INI文件 /// /// 节点名称[如[TypeName]] /// 键 /// 值 /// 文件路径 /// [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filepath); /// /// 读取INI文件 /// /// 节点名称 /// 键 /// 值 /// stringbulider对象 /// 字节大小 /// 文件路径 /// [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath); [DllImport("kernel32.dll", CharSet = CharSet.Auto)] private static extern uint GetPrivateProfileSection(string lpAppName, IntPtr lpReturnedString, uint nSize, string lpFileName); /// /// 自定义读取INI文件中的内容方法 /// /// 键 /// 值 /// public static string GetIniValue(string Section, string key, string filePath) { StringBuilder temp = new StringBuilder(1024); GetPrivateProfileString(Section, key, "", temp, 1024, filePath); return temp.ToString(); } public static void SetIniValue(string Section, string key, string value, string filePath) { WritePrivateProfileString(Section, key, value, filePath); } /// /// 根据section取所有key /// /// /// /// public static string[] ReadIniAllKeys(string section, string filePath) { UInt32 MAX_BUFFER = 32767; string[] items = new string[0]; IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char)); UInt32 bytesReturned = GetPrivateProfileSection(section, pReturnedString, MAX_BUFFER, filePath); if (!(bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0)) { string returnedString = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned); items = returnedString.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries); } Marshal.FreeCoTaskMem(pReturnedString); return items; } /// /// 读取INI文件中指定INI文件中的所有节点名称(Section) /// /// Ini文件 /// 所有节点,没有内容返回string[0] public static string[] INIGetAllSectionNames(string iniFile) { uint MAX_BUFFER = 32767; //默认为32767 string[] sections = new string[0]; //返回值 //申请内存 IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char)); uint bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, iniFile); if (bytesReturned != 0) { //读取指定内存的内容 string local = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned).ToString(); //每个节点之间用\0分隔,末尾有一个\0 sections = local.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries); } //释放内存 Marshal.FreeCoTaskMem(pReturnedString); return sections; } /// /// 获取INI文件中指定节点(Section)中的所有条目(key=value形式) /// /// Ini文件 /// 节点名称 /// 指定节点中的所有项目,没有内容返回string[0] public static string[] INIGetAllItems(string iniFile, string section) { //返回值形式为 key=value,例如 Color=Red uint MAX_BUFFER = 32767; //默认为32767 string[] items = new string[0]; //返回值 //分配内存 IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char)); uint bytesReturned = GetPrivateProfileSection(section, pReturnedString, MAX_BUFFER, iniFile); if (!(bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0)) { string returnedString = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned); items = returnedString.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries); } Marshal.FreeCoTaskMem(pReturnedString); //释放内存 return items; } } }