C#读取文本配置文件

  C#读取本文文件的方法很多,应用系统本身的方法就可以做到。

  现在的配置文件基本上都是JSON格式或者XML格式,传统格式:

[节点名称]
子项目名称1=值1
子项目名称2=值2
子项目名称3=值3
......

  上面的格式也是目前很多人所习惯的,网上答部分资料都是读取XML格式的,怎样读取这个格式的文件呢?

  效果图:

  如果节点的名称不为空,那么可以返回整个节点配置 的字典,方便快速检索,如果项目名称不为空,则只显示该项目的值。

  主要代码段:

        //读取配置文件
        public Dictionary<string, string> ReadConfig(string SFileName,string SSectionName)
        {
            Dictionary<string, string> DictConfig = new Dictionary<string, string>();//返回的字典
            Boolean BeSame = false;//是否属于同一个节点
            Boolean BeSectionTitle = false;//是否是节点名称
            //读入配置文件
            StreamReader SR = new StreamReader(SFileName, Encoding.Default);
            var S1 = SR.ReadToEnd();
            SR.Close();
            if (string.IsNullOrEmpty(SSectionName))
            {
                return DictConfig;
            }
            else
            {
                SSectionName = "[" + SSectionName + "]";
                //将内容分割到数组,按回车换行分割
                string[] SContext = S1.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                for (int i = 0; i < SContext.Length; i++)
                {
                    if (!string.IsNullOrEmpty(SContext[i]))
                    {
                        if (SContext[i].Substring(0, 1) == "[")
                        {
                            if (SSectionName == SContext[i])
                            {
                                BeSame = true;
                                BeSectionTitle = true;
                            }
                            else
                            {
                                BeSame = false;
                            }
                        }
                        else
                        {
                            BeSectionTitle = false;
                        }
                        if (BeSame && !BeSectionTitle)
                        {
                            string[] SItem = SContext[i].ToString().Split(new string[] { "=" }, StringSplitOptions.None);
                            DictConfig.Add(SItem[0], SItem[1]);
                        }
                    }
                }
                return DictConfig;
            }
        }

  用于测试的代码段:

            Dictionary<string, string> DictConfig = new Dictionary<string, string>();
            //获取配置文件路径和文件名称
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.DefaultExt = "txt";
            ofd.Title = "请选择配置文件路径和文件名称";
            ofd.ShowDialog();
            if (ofd.FileName != "")
            {
                string StrPath;
                StrPath = System.IO.Path.GetFullPath(ofd.FileName);
                DictConfig = ReadConfig(StrPath, textBox3.Text);
            }
            textBox2.Text = "";
            if (DictConfig.Count>=0)
            {
                    foreach (KeyValuePair<string, string> kvp in DictConfig)
                    {
                        if (string.IsNullOrEmpty(textBox4.Text))
                        {
                            textBox2.Text = textBox2.Text + kvp.Key + "=" + kvp.Value + Environment.NewLine;
                        }
                        else
                        {
                            if(kvp.Key == textBox4.Text)
                            {
                                textBox2.Text = kvp.Key + "=" + kvp.Value;
                                break;
                            }
                        }
                    }
            }

猜你喜欢

转载自blog.csdn.net/dawn0718/article/details/127831391