C#对txt文件的读写操作

向txt文档中写入内容 (创建文件并写入信息)

 // 向文本里面写入配置信息        
        public void WriteConfigToTxt(string strInfo)
        {
            DateTime dt = DateTime.Now;
            try
            {
                string user = "D:\\CCC";
                string configPath = user + "\\ABC";
                //判断该路径下文件夹是否存在,不存在的情况下新建文件夹
                if (!Directory.Exists(configPath))
                {
                    Directory.CreateDirectory(configPath);
                }
                //指定日志文件的目录
                string fname = configPath + "\\ABC.txt";
 
                //先将文本清空
                System.IO.File.WriteAllText(fname, string.Empty);
 
                //定义文件信息对象
                FileInfo finfo = new FileInfo(fname);
 
                if (!finfo.Exists)
                {
                    FileStream fs;
                    fs = File.Create(fname);
                    fs.Close();
                    finfo = new FileInfo(fname);
                }
 
                //创建只写文件流
                using (FileStream fs = finfo.OpenWrite())
                {
                    //根据上面创建的文件流创建写数据流
                    StreamWriter w = new StreamWriter(fs);
 
                    //设置写数据流的起始位置为文件流的末尾
                    w.BaseStream.Seek(0, SeekOrigin.End);
 
                    //写入内容
                    w.Write(dt.ToString() + "  "+ strInfo+ "\n\r");
 
                    //清空缓冲区内容,并把缓冲区内容写入基础流
                    w.Flush();
 
                    //关闭写数据流
                    w.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }
        }

读取txt文档中内容

 //读取txt文件,并返回文件中的内容
        public string ReadTxTContent()
        {
            try
            {
                string user = "D:\\CCC";                
                string contentPath = user + "\\ABC";
                if (Directory.Exists(contentPath))
                {
                    string strCon = string.Empty;
                    // 创建一个 StreamReader 的实例来读取文件
                    // using 语句也能关闭 StreamReader
                    contentPath = contentPath + "\\ABC.txt";
                    using (StreamReader sr = new StreamReader(contentPath))
                    {
                        string line;
                        // 从文件读取并显示行,直到文件的末尾 
                        while ((line = sr.ReadLine()) != null)
                        {
                            strCon += line + " ";
                        }
                    }
                    return strCon;
                }
                return null;
            }
            catch (Exception e)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }

C#实现txt文档读写操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐的叮小当

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值