/// <summary> /// 读取INI文件中指定KEY的字符串型值 /// </summary> /// <param name="section">节点名称</param> /// <param name="key">键名称</param> /// <param name="defaultValue">读取为空时返回的值</param> /// <returns>读取到的值</returns> public static string ReadItemValue(string iniPath, string section, string key, string defaultValue = "") { string value = defaultValue; const int SIZE = 1024 * 10; if (string.IsNullOrEmpty(section)) { throw new ArgumentException("必须指定节点名称", "section"); } if (string.IsNullOrEmpty(key)) { throw new ArgumentException("必须指定键名称(key)", "key"); } StringBuilder sb = new StringBuilder(SIZE); uint bytesReturned = IniFileHelper.GetPrivateProfileString(section, key, value, sb, SIZE, iniPath); if (bytesReturned != 0) { value = sb.ToString(); } sb = null; return(value); }
/// <summary> /// 获取INI文件中指定节点(Section)中的所有条目的Key列表 /// </summary> /// <param name="section">节点名称</param> /// <returns>如果没有内容,反回string[0]</returns> public static string[] ReadAllItemKeys(string iniPath, string section) { string[] keys = new string[0]; const int SIZE = 1024 * 200; if (string.IsNullOrEmpty(section)) { throw new ArgumentException("必须指定节点名称", "section"); } char[] chars = new char[SIZE]; uint bytesReturned = IniFileHelper.GetPrivateProfileString(section, null, null, chars, SIZE, iniPath); if (bytesReturned != 0) { // v1.2 modify 2016-10-21 support # remark. keys = (from key in new string(chars).Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries) where key.StartsWith("#") == false select key).ToArray(); } chars = null; return(keys); }