夜微凉、的博客

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
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 2015-12-25 23:02  夜、微凉  阅读(1414)  评论(0编辑  收藏  举报