源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

c#读写ini配置文件示例

  • 时间:2020-11-12 11:28 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:c#读写ini配置文件示例
其他人写的都是调用非托管kernel32.dll。我也用过 但是感觉兼容性有点不好 有时候会出现编码错误,毕竟一个是以前的系统一个是现在的系统。咱来写一个纯C#的ini格式配置文件读取,其实就是文本文件读写啦。但是我们要做的绝不仅仅是这样 是为了访问操作的方便 更是为了以后的使用。 都知道ini格式的配置文件里各个配置项 其实就是一行一行的文本 key跟value 用等号隔开。 像这样: grade=5 。 各个配置项又进行分组 同类型的放到一起 称之为section 以中括号([])区分。 像这样: [contact] qq=410910748 website=assassinx.cnblogs.com [score] math=85 Chinese=90 geographic=60 各个配置项的key在section内不可重复。 在这里我们为了方便 去掉section的概念 ,实际上也用不怎么到。那么这样一来就可以把个个配置项理解成一个dictionary结构,方便我们存取等操作 。至于为什么一定要使用dictionary 因为在测试时我发现存取过程中他不会打乱元素的存放顺序 晕 就这样啊。 我们要做到就是根据key去取value。还有就是需要注意到我们有时候需要在配置文件里写注释怎么办呢?就是以分号(;)开头的行。这个问题我们可以在程序里为他初始化特殊的key+序号的形式 ,写入的时候也同样的进行判断。 这整个过程就是: 程序开始时遍历所有行 如果以分号(;)开头则存储此行不作为配置解释,如果不是 则解释此行 并放到dictionary集合里去。访问时 根据key获取value 就这么简单。注意注释行的处理  还有更改配置存回去行的先后顺序必须保持原样。 好了开工吧:
[u]复制代码[/u] 代码如下:
public class Config {     public Dictionary<string, string> configData;     string fullFileName;     public Config(string _fileName)     {         configData = new Dictionary<string,string>();         fullFileName = Application.StartupPath + @"" + _fileName;         bool hasCfgFile =File.Exists(Application.StartupPath + @"" + _fileName);         if (hasCfgFile == false)         {             StreamWriter writer = new StreamWriter(File.Create(Application.StartupPath + @"" + _fileName), Encoding.Default);             writer.Close();         }         StreamReader reader = new StreamReader(Application.StartupPath + @"" + _fileName, Encoding.Default);         string line;         int indx = 0;         while ((line = reader.ReadLine()) != null)         {             if (line.StartsWith(";") || string.IsNullOrEmpty(line))                 configData.Add(";" + indx++, line);             else             {                 string[] key_value = line.Split('=');                 if (key_value.Length >= 2)                     configData.Add(key_value[0], key_value[1]);                 else                     configData.Add(";" + indx++, line);             }         }         reader.Close();     }     public string get(string key)     {         if (configData.Count <= 0)             return null;         else if(configData.ContainsKey(key))             return configData[key].ToString();         else             return null;     }     public void set(string key, string value)     {         if (configData.ContainsKey(key))             configData[key] = value;         else             configData.Add(key, value);     }     public void save()     {         StreamWriter writer = new StreamWriter(fullFileName,false,Encoding.Default);         IDictionaryEnumerator enu = configData.GetEnumerator();         while (enu.MoveNext())         {             if (enu.Key.ToString().StartsWith(";"))                 writer.WriteLine(enu.Value);             else                 writer.WriteLine(enu.Key + "=" + enu.Value);         }         writer.Close();     } }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部