C#读取和保存ini配置文件,不使用API,自己实现配置ini配置文件的读写

引用都是一些基础引用,自己添加就行。凑合着可用,请自行根据情况优化。注意:使用时请注意编码问题


                /// <summary>
                /// 获取配置文件
                /// </summary>
                /// <param name="path">路径</param>
                /// <param name="group">组名</param>
                /// <param name="key">键</param>
                /// <returns>值</returns>
               public static string GetConfigValue(string path,string group,string key)
               {
                        if (!File.Exists(path)) return null;
                        group =string.Concat("[" , group , "]");
                        StreamReader sr = new StreamReader(path,Encoding.Default);//使用时请注意字符编码问题
                        bool group_bl = false;
                        string value = null;
                        while(!sr.EndOfStream)
                        {
                                string line = sr.ReadLine().Trim();
                                if (string.IsNullOrEmpty(line)) continue;
                                if (line[0] == '[')
                                {
                                        if(group_bl)
                                        {
                                                break;
                                        }
                                        if (line == group)
                                        {
                                                group_bl = true;
                                                continue;
                                        }
                                }
                                if(group_bl)
                                {
                                        string[] strkv = line.Split('=');
                                        if (strkv.Length < 2) continue;
                                        if(strkv[0].Trim()==key)
                                        {
                                                value= strkv[1];
                                                break;
                                        }
                                }
                        }

                        sr.Close();
                        sr.Dispose();
                        return value.Trim();
               }

                public static Dictionary<string,string> GetConfigValue(string path, string group)
                {
                        if (!File.Exists(path)) return null;
                        group = string.Concat("[", group, "]");
                        StreamReader sr = new StreamReader(path, Encoding.Default);//使用时请注意字符编码问题
                        bool group_bl = false;
                        Dictionary<string, string> result = new Dictionary<string, string>();
                        while (!sr.EndOfStream)
                        {
                                string line = sr.ReadLine().Trim();
                                if (string.IsNullOrEmpty(line)) continue;
                                if (line[0] == '[')
                                {
                                        if (group_bl)
                                        {
                                                break;
                                        }
                                        if (line == group)
                                        {
                                                group_bl = true;
                                                continue;
                                        }
                                }
                                if (group_bl)
                                {
                                        string[] strkv = line.Split('=');
                                        if (strkv.Length < 2) continue;
                                        result[strkv[0].Trim()] = strkv[1].Trim();
                                }
                        }

                        sr.Close();
                        sr.Dispose();
                        return result;
                }

                public static Dictionary<string,Dictionary<string, string>> GetConfigValue(string path)
                {
                        if (!File.Exists(path)) return null;
                        StreamReader sr = new StreamReader(path, Encoding.Default);//使用时请注意字符编码问题
                        Dictionary<string, Dictionary<string, string>> result = new Dictionary<string, Dictionary<string, string>>();
                        Dictionary<string, string> curgroup=null;
                        while (!sr.EndOfStream)
                        {
                                string line = sr.ReadLine().Trim();
                                if (string.IsNullOrEmpty(line)) continue;
                                if (line[0] == '[' && line[line.Length - 1] == ']')
                                {
                                        curgroup = new Dictionary<string, string>();
                                        string key = line.Remove(line.Length - 1).Substring(1);
                                        result[key] = curgroup;
                                        continue;
                                }
                                if(curgroup!=null)
                                {
                                        string[] strkv = line.Split('=');
                                        if (strkv.Length < 2) continue;
                                        curgroup[strkv[0].Trim()] = strkv[1].Trim();
                                }
                        }

                        sr.Close();
                        sr.Dispose();
                        return result;
                }


                /// <summary>
                /// 设置配置文件
                /// </summary>
                /// <param name="path">路径</param>
                /// <param name="group">组</param>
                /// <param name="key">键</param>
                /// <param name="value">值</param>
                public static void SetConfigValue(string path, string group, string key,string value)
                {
                        group = string.Concat("[" , group ,"]");
                        Dictionary<string, Dictionary<string, string>> gkv = new Dictionary<string, Dictionary<string, string>>();
                        if (File.Exists(path))
                        {
                                StreamReader sr = new StreamReader(path, Encoding.Default);//使用时请注意字符编码问题
                                string curgroup = null;
                                while (!sr.EndOfStream)
                                {
                                        string line = sr.ReadLine().Trim();
                                        if (string.IsNullOrEmpty(line))
                                        {
                                                continue;
                                        }
                                        if (line[0] == '['&&line[line.Length-1]==']')
                                        {
                                                curgroup = line;
                                                gkv[curgroup] = new Dictionary<string, string>();
                                                continue;
                                        }
                                        if(curgroup!=null)
                                        {
                                                string[] strkv = line.Split('=');
                                                if(strkv.Length>=2)
                                                {
                                                        gkv[curgroup][strkv[0]] = strkv[1];
                                                }
                                                else
                                                {
                                                        gkv[curgroup][strkv[0]] = string.Empty;
                                                }
                                        }
                                }
                                sr.Close();
                                sr.Dispose();
                        }
                        if(!gkv.ContainsKey(group))
                        {
                                gkv[group] = new Dictionary<string, string>();
                        }
                        gkv[group][key] = value;

                        using (StreamWriter sw = new StreamWriter(path, false, Encoding.Default))
                        {
                                foreach (var temp in gkv)
                                {
                                        sw.WriteLine(temp.Key);
                                        foreach (var temp1 in temp.Value)
                                        {
                                                sw.WriteLine("{0}={1}",temp1.Key,temp1.Value);
                                        }
                                }
                        }
                }
                public static void SetConfigValue(string path, string group, Dictionary<string,string> pairs)
                {
                        group = string.Concat("[", group, "]");
                        Dictionary<string, Dictionary<string, string>> gkv = new Dictionary<string, Dictionary<string, string>>();
                        if (File.Exists(path))
                        {
                                StreamReader sr = new StreamReader(path, Encoding.Default);//使用时请注意字符编码问题
                                string curgroup = null;
                                while (!sr.EndOfStream)
                                {
                                        string line = sr.ReadLine().Trim();
                                        if (string.IsNullOrEmpty(line))
                                        {
                                                continue;
                                        }
                                        if (line[0] == '[' && line[line.Length - 1] == ']')
                                        {
                                                curgroup = line;
                                                gkv[curgroup] = new Dictionary<string, string>();
                                                continue;
                                        }
                                        if (curgroup != null)
                                        {
                                                string[] strkv = line.Split('=');
                                                if (strkv.Length >= 2)
                                                {
                                                        gkv[curgroup][strkv[0]] = strkv[1];
                                                }
                                                else
                                                {
                                                        gkv[curgroup][strkv[0]] = string.Empty;
                                                }
                                        }
                                }
                                sr.Close();
                                sr.Dispose();
                        }
                        if (gkv.ContainsKey(group))
                        {
                              foreach(var pair in pairs)
                              {
                                        gkv[group][pair.Key] = pair.Value;
                              }
                        }
                        else
                        {
                                gkv[group] = pairs;
                        }

                        using (StreamWriter sw = new StreamWriter(path, false, Encoding.Default))
                        {
                                foreach (var temp in gkv)
                                {
                                        sw.WriteLine(temp.Key);
                                        foreach (var temp1 in temp.Value)
                                        {
                                                sw.WriteLine("{0}={1}", temp1.Key, temp1.Value);
                                        }
                                }
                        }
                }
                public static void SetConfigValue(string path, Dictionary<string,Dictionary<string, string>> pairs)
                {
                        StringBuilder stb = new StringBuilder();
                        Dictionary<string, Dictionary<string, string>> gkv = new Dictionary<string, Dictionary<string, string>>();
                        if (File.Exists(path))
                        {
                                StreamReader sr = new StreamReader(path, Encoding.Default);//使用时请注意字符编码问题
                                string curgroup = null;
                                while (!sr.EndOfStream)
                                {
                                        string line = sr.ReadLine().Trim();
                                        if (string.IsNullOrEmpty(line))
                                        {
                                                continue;
                                        }
                                        if (line[0] == '[' && line[line.Length - 1] == ']')
                                        {
                                                curgroup = line;
                                                gkv[curgroup] = new Dictionary<string, string>();
                                                continue;
                                        }
                                        if (curgroup != null)
                                        {
                                                string[] strkv = line.Split('=');
                                                if (strkv.Length == 2)
                                                {
                                                        gkv[curgroup][strkv[0]] = strkv[1];
                                                }
                                                else
                                                {
                                                        gkv[curgroup][strkv[0]] = string.Empty;
                                                }
                                        }
                                }
                                sr.Close();
                                sr.Dispose();
                        }

                        if (gkv.Count>0)
                        {
                               foreach(var pair in pairs)
                                {
                                        if(gkv.ContainsKey(pair.Key))
                                        {
                                                foreach(var pair1 in pair.Value)
                                                {
                                                        gkv[pair.Key][pair1.Key] = pair1.Value;
                                                }
                                        }
                                        else
                                        {
                                                gkv[pair.Key] = pair.Value;
                                        }
                                }
                        }
                        else
                        {
                                gkv.Clear();
                                gkv = pairs;
                        }

                        using (StreamWriter sw = new StreamWriter(path, false, Encoding.Default))
                        {
                                foreach (var temp in gkv)
                                {
                                        sw.WriteLine(temp.Key);
                                        foreach (var temp1 in temp.Value)
                                        {
                                                sw.WriteLine("{0}={1}", temp1.Key, temp1.Value);
                                        }
                                }
                        }
                }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值