国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[C#]INI文件控制類

2019-11-14 13:57:17
字體:
來源:轉載
供稿:網友

INI文件常用于保存各類設置或本地化文本,大概格式如下:

[Section]key=value

然而.NET框架似乎并沒有提供一個實用的工具來操作它,或許是因為MS想讓我們都使用Settings類控制的config文件?
但是出于多種原因,我還是不太喜歡用Settings類以及這個xml格式的config文件。

幸運的是,有兩個Win32API可以幫我們完成INI文件的控制:
WritePRivateProfileString
GetPrivateProfileString

但是非常尷尬的是這倆一個能寫入中文,另一個卻讀不好中文。。。

于是只好自己動手豐衣足食了,謹記于此以備日后又有所需:

  1 abstract class ConfigurationBase  2 {  3     public abstract string Path { get; }  4   5     public abstract string Section { get; }  6   7     /// <summary>  8     /// 指定好編碼格式就能支持各種語言文字了  9     /// </summary> 10     private readonly Encoding encoding = Encoding.UTF8; 11  12     public void Clear() 13     { 14         File.Delete(Path); 15     } 16  17     public void Save() 18     { 19         File.WriteAllLines(Path, lines.ToArray(), encoding); 20     } 21  22     private readonly List<string> lines; 23  24     protected ConfigurationBase() 25     { 26         if (File.Exists(Path)) 27         { 28             lines = new List<string>( 29                 File.ReadAllLines(Path, encoding)); 30         } 31         else 32         { 33             lines = new List<string>(); 34         } 35     } 36  37     protected string Get(string key, string defaultVal) 38     { 39         if (lines.Count != 0) 40         { 41             string sectionLine = String.Format("[{0}]", Section); 42             string keyLine = String.Format("{0}=", key); 43             Regex otherSection = new Regex(@"^/[[^/]+]/]$", RegexOptions.Compiled); 44  45             bool inSection = false; 46             foreach (string line in lines) 47             { 48                 if (sectionLine == line) 49                 { 50                     inSection = true; 51                 } 52                 else if (otherSection.IsMatch(line)) 53                 { 54                     if (inSection) break; 55                 } 56                 else if (inSection && line.StartsWith(keyLine)) 57                 { 58                     return line.Substring(keyLine.Length); 59                 } 60             } 61         } 62         return defaultVal; 63     } 64  65     protected void Set(string key, string value) 66     { 67         string sectionLine = String.Format("[{0}]", Section); 68         string keyLine = String.Format("{0}=", key); 69         Regex otherSection = new Regex(@"^/[[^/]+]/]$", RegexOptions.Compiled); 70         string valueLine = String.Format("{0}{1}", keyLine, value); 71  72         bool inSection = false; 73         for (int i = 0; i < lines.Count; i++) 74         { 75             if (sectionLine == lines[i]) 76             { 77                 inSection = true; 78             } 79             else if (otherSection.IsMatch(lines[i])) 80             { 81                 if (inSection) 82                 { 83                     lines.Insert(i, valueLine); 84                     break; 85                 } 86             } 87             else if (inSection && lines[i].StartsWith(keyLine)) 88             { 89                 lines[i] = valueLine; 90             } 91         } 92         if (inSection) 93         { 94             lines.Add(valueLine); 95         } 96         else 97         { 98             lines.Add(sectionLine); 99             lines.Add(valueLine);100         }101     }102 }

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 博罗县| 宣化县| 依兰县| 运城市| 上饶市| 右玉县| 台中市| 澎湖县| 松原市| 蕉岭县| 滨州市| 巴马| 麻栗坡县| 金川县| 肃宁县| 丽水市| 东阿县| 佛山市| 惠东县| 宁都县| 噶尔县| 旌德县| 三都| 通许县| 灵丘县| 蒙山县| 体育| 东乡县| 潮州市| 阿克| 庆安县| 营山县| 浦县| 宁化县| 安徽省| 昌都县| 霍林郭勒市| 务川| 济阳县| 贡嘎县| 大竹县|