• 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
  • 相关阅读:
    How to install VXDIAG Honda, Toyota and JLR SDD software
    16% off MPPS V16 ECU tuning tool for EDC15 EDC16 EDC17
    Cummins INSITE locked and ask for verification code
    How to use BMW Multi Tool 7.3 to replace lost key for BMW X1
    Bleed Brake Master Cylinder with Intelligent Tester IT2
    Porsche Piwis Tester II “No VCI has been detected”,how to do?
    Creader VIII VS. Creader VII+
    How to solve GM MDI cannot complete the installation
    汽车OBD2诊断程序开发 (原文转载,思路很清晰!)
    汽车节温器单片机开发思路
  • 原文地址:https://www.cnblogs.com/wwj1992/p/5077227.html
Copyright © 2020-2023  润新知