private static IniSection ReadIniFileSection(string filePath, string section, System.Text.Encoding encoding) { section = section.Trim().ToLower(); // ファイル名・セクション名がない場合は終了する if (filePath == string.Empty || section == string.Empty) { return null; } // 指定したファイルが存在しない場合は Null を返す if (!System.IO.File.Exists(filePath)) { return null; } using (System.IO.StreamReader cReader = new System.IO.StreamReader(filePath, encoding)) { try { while (cReader.Peek() > -1) { //string target = cReader.ReadLine().TrimStart(); string target = cReader.ReadLine().Trim(); // セクションの始まりかどうか判断する if (IsSectionBegin(target, section)) { IniSection hSection = new IniSection(); while (cReader.Peek() > -1) { //target = cReader.ReadLine().TrimStart(); target = cReader.ReadLine().Trim(); // セクションの終わりかどうか判断する if (IsSectionEnd(target)) { return hSection; } // イコールがあるかどうか判断する int iLength = target.IndexOf('='); if (iLength >= 1) { //string key = target.Substring(0, iLength - 1).TrimEnd(); string key = target.Substring(0, iLength - 1).Trim(); string value = TrimDoubleQuote(target.Substring(iLength + 1).Trim()); hSection.Add(new IniItem(key, value)); } } return hSection; } } } catch (System.Exception ex) { throw; } finally { if (cReader != null) { cReader.Close(); } } } return null; }
private static IniSection ReadIniFileSection(string filePath, string section, System.Text.Encoding encoding) { section = section.Trim().ToLower(); // ファイル名・セクション名がない場合は終了する if (filePath == string.Empty || section == string.Empty) { return(null); } // 指定したファイルが存在しない場合は Null を返す if (!System.IO.File.Exists(filePath)) { return(null); } using (System.IO.StreamReader cReader = new System.IO.StreamReader(filePath, encoding)) { try { while (cReader.Peek() > -1) { //string target = cReader.ReadLine().TrimStart(); string target = cReader.ReadLine().Trim(); // セクションの始まりかどうか判断する if (IsSectionBegin(target, section)) { IniSection hSection = new IniSection(); while (cReader.Peek() > -1) { //target = cReader.ReadLine().TrimStart(); target = cReader.ReadLine().Trim(); // セクションの終わりかどうか判断する if (IsSectionEnd(target)) { return(hSection); } // イコールがあるかどうか判断する int iLength = target.IndexOf('='); if (iLength >= 1) { //string key = target.Substring(0, iLength - 1).TrimEnd(); string key = target.Substring(0, iLength - 1).Trim(); string value = TrimDoubleQuote(target.Substring(iLength + 1).Trim()); hSection.Add(new IniItem(key, value)); } } return(hSection); } } } catch (System.Exception ex) { throw; } finally { if (cReader != null) { cReader.Close(); } } } return(null); }