C# 读写txt文档

来源:互联网 发布:深入理解php内核 pdf 编辑:程序博客网 时间:2024/06/05 20:30

在做项目的时候经常要修改配置,所以不免要读取文件:

下面以读写字典为例,读写tex文件:


        private void savefile()        {            string path="F:\\ak.txt";            FileStream fs = new FileStream(path, FileMode.Create);            StreamWriter sw = new StreamWriter(fs);            //开始写入            string s = "";            foreach (KeyValuePair<decimal, string> kvp in content)            {                s = kvp.Key.ToString() +"+"+ kvp.Value.ToString();                sw.WriteLine(s);            }            //清空缓冲区            sw.Flush();            //关闭流            sw.Close();            fs.Close();        }
private void openfile()        {            string path = "F:\\ak.txt";            try            {                StreamReader sr = new StreamReader(path, Encoding.UTF8);                String line;                while ((line = sr.ReadLine()) != null)                {                    string[] sArray = line.Split('+');                    decimal u = decimal.Parse(sArray[0]);                    string c = sArray[1];                    content.Add(u, c);                    this.comboBox1.Items.Add(c);                }            }            catch { }



0 0