zoukankan      html  css  js  c++  java
  • C# 文件的读取、写入和删除

    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
  • 相关阅读:
    [转]项目需求范围管理
    JavaScript 王者归来
    [转]大文件上传组件
    [转]使用vs2005自带的sql2005 express
    JS判断Caps Lock
    [转]Web项目管理思考
    [转]JS严格验证身份证
    两分钟用C#搭建IE BHO勾子, 窃取密码
    asp.net性能提升十个方法(Microsoft)
    [转]Asp.net 将js文件打包进dll 方法
  • 原文地址:https://www.cnblogs.com/wwj1992/p/5077227.html
Copyright © 2011-2022 走看看