From c718d32bb03eca3a1eefbdc98f6a413725173402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=93=9D=E7=82=B9lilac?= <1617859183@qq.com> Date: Thu, 15 Oct 2020 15:03:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E9=83=A8=E5=88=86=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BulePointLilac.Methods/IniReader.cs | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/ContextMenuManager/BulePointLilac.Methods/IniReader.cs b/ContextMenuManager/BulePointLilac.Methods/IniReader.cs index 8729cf2..bc77f21 100644 --- a/ContextMenuManager/BulePointLilac.Methods/IniReader.cs +++ b/ContextMenuManager/BulePointLilac.Methods/IniReader.cs @@ -10,8 +10,8 @@ public class IniReader { public IniReader(StringBuilder sb) { - if(string.IsNullOrWhiteSpace(sb.ToString())) return; - List lines = sb.ToString().Split(new[] { Environment.NewLine }, + if (string.IsNullOrWhiteSpace(sb.ToString())) return; + List lines = sb.ToString().Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();//拆分为行 lines.ForEach(line => line.Trim()); ReadLines(lines); @@ -19,13 +19,13 @@ public IniReader(StringBuilder sb) public IniReader(string filePath) { - if(!File.Exists(filePath)) return; + if (!File.Exists(filePath)) return; List lines = new List(); - using(StreamReader reader = new StreamReader(filePath, EncodingType.GetType(filePath))) - while(!reader.EndOfStream) + using (StreamReader reader = new StreamReader(filePath, EncodingType.GetType(filePath))) + while (!reader.EndOfStream) { string line = reader.ReadLine().Trim(); - if(line != string.Empty) lines.Add(line); + if (line != string.Empty) lines.Add(line); } ReadLines(lines); } @@ -39,26 +39,26 @@ private void ReadLines(List lines) line => line.StartsWith(";")//移除注释 || (!line.StartsWith("[") && !line.Contains("=")));//移除非section行且非key行 - if(lines.Count == 0) return; + if (lines.Count == 0) return; List indexs = new List { 0 }; - for(int i = 1; i < lines.Count; i++) + for (int i = 1; i < lines.Count; i++) { - if(lines[i].StartsWith("[")) indexs.Add(i);//获取section行号 + if (lines[i].StartsWith("[")) indexs.Add(i);//获取section行号 } indexs.Add(lines.Count); - for(int i = 0; i < indexs.Count - 1; i++) + for (int i = 0; i < indexs.Count - 1; i++) { string section = lines[indexs[i]]; int m = section.IndexOf(']') - 1; - if(m < 0) continue; + if (m < 0) continue; section = section.Substring(1, m); - if(rootDic.ContainsKey(section)) continue; + if (rootDic.ContainsKey(section)) continue; var keyValues = new Dictionary(StringComparer.OrdinalIgnoreCase); rootDic.Add(section, keyValues); - for(int j = indexs[i] + 1; j < indexs[i + 1]; j++) + for (int j = indexs[i] + 1; j < indexs[i + 1]; j++) { int k = lines[j].IndexOf('='); string key = lines[j].Substring(0, k).TrimEnd(); @@ -70,8 +70,8 @@ private void ReadLines(List lines) public string GetValue(string section, string key) { - if(rootDic.TryGetValue(section, out Dictionary sectionDic)) - if(sectionDic.TryGetValue(key, out string value)) + if (rootDic.TryGetValue(section, out Dictionary sectionDic)) + if (sectionDic.TryGetValue(key, out string value)) return value; return string.Empty; }