| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- using System;
- using System.Runtime.InteropServices;
- using System.Text;
- namespace ErpServer
- {
- public class INI
- {
- /// <summary>
- /// 获取所有节点名称(Section)
- /// </summary>
- /// <param name="lpszReturnBuffer">存放节点名称的内存地址,每个节点之间用\0分隔</param>
- /// <param name="nSize">内存大小(characters)</param>
- /// <param name="lpFileName">Ini文件</param>
- /// <returns>内容的实际长度,为0表示没有内容,为nSize-2表示内存大小不够</returns>
- [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
- private static extern uint GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, uint nSize, string lpFileName);
- /// <summary>
- /// 写入INI文件
- /// </summary>
- /// <param name="section">节点名称[如[TypeName]]</param>
- /// <param name="key">键</param>
- /// <param name="val">值</param>
- /// <param name="filepath">文件路径</param>
- /// <returns></returns>
- [DllImport("kernel32")]
- private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
- /// <summary>
- /// 读取INI文件
- /// </summary>
- /// <param name="section">节点名称</param>
- /// <param name="key">键</param>
- /// <param name="def">值</param>
- /// <param name="retval">stringbulider对象</param>
- /// <param name="size">字节大小</param>
- /// <param name="filePath">文件路径</param>
- /// <returns></returns>
- [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);
- /// <summary>
- /// 自定义读取INI文件中的内容方法
- /// </summary>
- /// <param name="Section">键</param>
- /// <param name="key">值</param>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- /// 根据section取所有key
- /// </summary>
- /// <param name="section"></param>
- /// <param name="filePath"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 读取INI文件中指定INI文件中的所有节点名称(Section)
- /// </summary>
- /// <param name="iniFile">Ini文件</param>
- /// <returns>所有节点,没有内容返回string[0]</returns>
- 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;
- }
- /// <summary>
- /// 获取INI文件中指定节点(Section)中的所有条目(key=value形式)
- /// </summary>
- /// <param name="iniFile">Ini文件</param>
- /// <param name="section">节点名称</param>
- /// <returns>指定节点中的所有项目,没有内容返回string[0]</returns>
- 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;
- }
- }
- }
|