ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C#对txt文件的读写操作

2022-07-14 17:06:54  阅读:165  来源: 互联网

标签:文件 fs string 读写操作 C# contentPath configPath fname txt


        /// <summary>
        /// 向文本里面写入配置信息
        /// </summary>       
        public void WriteConfigToTxt(int times, int days, string delPath)
        {
            try
            {
                string user = Environment.GetFolderPath(Environment.SpecialFolder.Templates);
                string configPath = user + "\\ConfigFile";
                //string logPath = "c:\\LogFile";
                //判断该路径下文件夹是否存在,不存在的情况下新建文件夹
                if (!Directory.Exists(configPath))
                {
                    Directory.CreateDirectory(configPath);
                }
                //指定日志文件的目录
                string fname = configPath + "\\ConfigFile.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(times.ToString() + "\n" + days + "\n" + delPath + "\n\r");

                    //清空缓冲区内容,并把缓冲区内容写入基础流
                    w.Flush();

                    //关闭写数据流
                    w.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }


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

 

标签:文件,fs,string,读写操作,C#,contentPath,configPath,fname,txt
来源: https://www.cnblogs.com/ahao214/p/16478370.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有