夜微凉、的博客

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  76 随笔 :: 24 文章 :: 22 评论 :: 24万 阅读
复制代码
class Program 
    {
        static void Main(string[] args)
        {
            EmployeeDAL DAL = new EmployeeDAL();
            List<Sys_Employee> list = DAL.GetAll().ToList(); 
            //WriteTxt(list);
            //DeleDirFile();
            Console.WriteLine("请输入文件路径!");
            string path = Console.ReadLine();
            ReadTxt(path);
        }

        #region   对文件的操作

        //写文件
        public static void WriteTxt(List<Sys_Employee> Emp)
        {
            string path = @"F:\CreateDirTxt";

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            foreach (var emp in Emp)
            {
                //创建文件流
                FileStream Stream = new FileStream(@"F:\CreateDirTxt\" + emp.EmpName + "信息文本.txt", FileMode.Create);  
                StreamWriter Writer = new StreamWriter(Stream);
                //向流中写入内容
                Writer.Write(string.Format("姓名是:{0},性别是:{1},地址是:{2}", EmpName, emp.EmpSex, emp.EmpAddress));
                //清空缓存
                Writer.Flush();
                //关闭
                Writer.Close();
                Console.WriteLine("正在创建  " + emp.EmpName + "  的信息文本");
            }
            Console.WriteLine("创建完成 O(∩_∩)O");
            Console.ReadLine();
        } 
        //删文件
        public static void DeleDirFile()
        { 
            string path = @"F:\CreateDirTxt\";
            if (Directory.Exists(path))
            {
                //获得文件夹数组
                string[] Directorlenght = Directory.GetDirectories(path);
                //获得文件数组
                string[] filelength = Directory.GetFiles(path);
                //遍历删除文件夹
                foreach (string lst in Directorlenght)
                {
                    Directory.Delete(lst);
                }
                //遍历删除文件
                foreach (string lst in filelength)
                {
                    int Index = lst.LastIndexOf("\\") + 1;
                    string EmpName = lst.Substring(Index, lst.Length - Index);
                    File.Delete(lst);
                    Console.WriteLine("文件 -"+EmpName+"- 删除成功");
                }
                Console.WriteLine("完成!  O(∩_∩)O");
            }
            else
            {
                Console.WriteLine("文件或者文件夹不存在,请重新查看");
            }
            Console.ReadLine();
        }
        //读文件 -按照每行进行读取
        public static void ReadTxt(string FilePath)
        {
            string path = @FilePath;    //路径
            if (File.Exists(@FilePath))   /判断路径是否存在
            {
                StreamReader Reader = new StreamReader(path,Encoding.UTF8);
                string linetext;
                while ((linetext=Reader.ReadLine())!=null)
                {
                    Console.WriteLine(linetext); 
                }
            }
            else
            {
                Console.WriteLine("该文件不存在!");
            }
            Console.ReadLine();
        }

        #endregion
复制代码

 

posted on   夜、微凉  阅读(1432)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示