• C#操作文件和文件夹的类


    下面对处理文件常用的类做了一些介绍,还算详细吧,主要包括:DirectoryInfo,Directory,Path,FileInfo,File,FileStream。

    private void button2_Click(object sender, EventArgs e)
            {           
                TimeSpan ts = new TimeSpan();
                DateTime dtStart = DateTime.Now;
                string currFile = textBox1.Text;
                string Json = File.ReadAllText(currFile).TrimStart();//将文件内容转换成字符串类型输出
                int index = Json.IndexOf('[');//判断某个字符在字符串里面的第几个位置
                #region DirectoryInfo 操作文件夹
                DirectoryInfo d = new DirectoryInfo(@"D:ATSApplicationVS2017Source Code"); d.Create();    \创建目录:
                d.Delete();//删除目录:
                d.MoveTo(@"D:ATSApplicationVS2017Source Code");//移动目录:
                bool b = d.Exists; //目录是否存在:
                string fullname=d.FullName;//获得目录全名:
                FileInfo[] f = d.GetFiles();//获得子文件!对象信息!: 
                d.GetFiles("*.exe");//返回的是文件对象数组,内容更详细,只获取exe的文件
                DirectoryInfo[] dr = d.GetDirectories(); //获得子目录:
                #endregion DirectoryInfo 操作文件夹
                #region Directory操作文件夹
                Directory.CreateDirectory(@"D:ATSApplicationVS2017Source CodeJayoda1019	estTEST");//创建文件夹
                Directory.Delete(@"D:ATSApplicationVS2017Source CodeJayoda1019	estTEST", true);//删除TEST目录,true将删除整个目录,即使该目录下有文件或子目录;若为false,则仅当目录为空时才可删除。
                File.Move(@"c:	empuploadsNewDirectory", @"c:	empuploadsBackUp");//移动文件夹
    
                //下面的代码读出c:	empuploads目录下的所有子目录,并将其存储到字符串数组中。
                string[] Directorys;
                Directorys = Directory.GetDirectories(@"c:	empuploads");
    
                //获取当前目录下的所有文件方法,下面的代码读出c:	empuploads目录下的所有文件,并将其存储到字符串数组中
                string[] Files;
                Files = Directory.GetFiles(@"c:	empuploads");
    
                Directory.Exists(@"c:	empuploads");    //判断目录是否存在
                Directory.GetDirectoryRoot(@"c:	empuploads");//获取根目录
    
                Directory.GetParent(@"c:	empuploads"); //获取上一级目录
                #endregion
                #region Path处理文件
                string file_name = Path.GetFileName(currFile);//从一个地址中解析出文件名
                Path.GetFileNameWithoutExtension(currFile);//从路径字符串中得到文件名(不带扩展名)
                Path.GetExtension(currFile);//从文件路径字符串中得到文件的扩展名
                Path.GetDirectoryName(currFile);//得到文件夹的路径
                Path.GetFullPath(currFile);//得到包括文件名和扩展名的全路径名。
                //合并路径
                string Str1 = @"C:UsersAdministrator";
                string Str2 = @"Desktop测试文件.txt";
                Path.Combine(Str1, Str2);
                #endregion
                #region FileInfo处理文件
                FileInfo fi = new FileInfo(currFile);
                DateTime dt = fi.CreationTime;  //获取或设置当前文件或目录的创建时间。 
                string directory = fi.DirectoryName;    //获取表示目录的完整路径的字符串
                bool IfExists = fi.Exists;  //获取指示文件是否存在
                string Extension = fi.Extension;    //获取表示文件扩展名部分的字符串
                string fullName = fi.FullName;  //获取目录或文件的完整目录
                string Name = fi.Name;  //获取文件名
                fi.CopyTo(@"D:ATSApplicationVS2017Source CodeJayoda1019	est	est.JSON", true);//将文件copy到指定路径,并且覆盖之前的文件
                fi.MoveTo(@"D:ATSApplicationVS2017Source CodeJayoda1019	est	est1.JSON");//将文件move到指定路径,并且覆盖之前的文件
                FileStream fs=fi.Create();  //创建一个文件
                fi.Delete();    //删除一个文件
                #endregion                            ////////////////////////////////////////////////////////////////////////////////
                #region File处理文件
                File.Create(@"D:TestDebug1测试.txt");//创建文件
                File.Copy(@"c:	empuploads
    ewFile.txt", @"c:	empuploadsBackUp.txt", true);//copy文件        
                File.Delete(@"c:	empuploads
    ewFile.txt");
                File.Move(@"C:UsersscottzhangNEW.txt", @"D:ATSApplicationVS2017Source CodeJayoda1019	estNEW.txt");
                File.Exists(@"c:	empuploads
    ewFile.txt");//判断文件是否存在
                #endregion
                #region FileStream读写文件,代码会报错
                //1.创建一个    中国.txt
                string txt = "中国是世界上人口第一大国。中国是世界上最幸福的国家之一。";
                //一、创建一个文件流
                FileStream fs1 = new FileStream(@"D:ATSApplicationVS2017Source CodeJayoda1019	estNEW.txt", FileMode.Create, FileAccess.Write);
                byte[] buffer = Encoding.UTF8.GetBytes(txt);
                //二、读文件或者写文件
                //参数1:表示要把哪个byte[]数组中的内容写入到文件
                //参数2:表示要从该byte[]数组的第几个下标开始写入,一般都是0
                //参数3:要写入的字节的个数。
                fs.Write(buffer, 0, buffer.Length);
                ////三、关闭文件流
                ////清空缓冲区
                //fs.Flush();
                //fs.Close();
                //四、释放相关资源
                fs.Dispose();//自动调用close和flush方法
                Console.WriteLine("ok");
                Console.ReadKey();
                //1.创建一个    中国.txt
                string txt1 = "中国是世界上人口第一大国。中国是世界上最幸福的国家之一。中国是四大文明古国之一。中国有个杨中科。";
                //一、创建一个文件流
                //当把一个对象放到using()中的时候,当超出using的作用于范围后,会自动调用该对象的Dispose()f方法。
                using (FileStream fs2 = new FileStream(@"c:中国.txt", FileMode.Create, FileAccess.Write))
                {
                    byte[] buffer1 = Encoding.UTF8.GetBytes(txt);
                    //二、读文件或者写文件
                    //参数1:表示要把哪个byte[]数组中的内容写入到文件
                    //参数2:表示要从该byte[]数组的第几个下标开始写入,一般都是0
                    //参数3:要写入的字节的个数。
                    fs.Write(buffer, 0, buffer.Length);
                }
                Console.WriteLine("ok");
                Console.ReadKey();
                #endregion
    
            }

    原文链接:https://www.cnblogs.com/ziqiumeng/p/10537538.html

    C#操作文件及文件夹的方式说明,主要用到的类:Directory,File。命名空间:using System.IO;

    //1) 相对路径转绝对路径
    string fullfolder = HttpContext.Current.Server.MapPath(folder);
    
    //2) 文件移动(改名)
    File.Move(Server.MapPath("/a.txt"), Server.MapPath("/b.txt"));
    
    //3) 文件复制
    File.Copy(Server.MapPath("/a.txt"), Server.MapPath("/b.txt"), true);
    
    //4) 文件是否存在
    File.Exists(filefullname)
    
    //5) 目录是否存在
    Directory.Exists(fullfolder))
    
    //6) 创建目录
    Directory.CreateDirectory(fullfolder);
    
    //7) 目录移动
    Directory.Move
    
    //8) 读取文本文件
    StreamReader srd = File.OpenText(fullfilename);
    srd.ReadToEnd();
    srd.Close();
    srd.Dispose();
    
    //9) 写文件
    StreamWriter swr = File.CreateText(Server.MapPath("test.txt"));
    swr.Write("message");
    swr.Close();
    swr.Dispose();
    
    //10)删除文件
    // 删除硬盘上的文件
    if (File.Exists(filefullname))
    {
    File.Delete(filefullname);
    }
    
    //11)目录遍历
    public void ListFiles(string pathname)
    {
    // 所有目录与文件
    string[] subDirs = Directory.GetDirectories(pathname);
    string[] subFiles = Directory.GetFiles(pathname);
    
    foreach (string subDir in subDirs)
    {
    ListFiles(subDir);
    }
    
    // 所有文件
    foreach (string subFile in subFiles)
    {
    string filename = Path.GetFileName(subFile);
    }
    }
    
    //12)文件修改时间
    FileInfo fi = new FileInfo(@"c:	est.txt");
    DateTime writetime = fi.LastWriteTime;
    
    //13)从含路径的文件名中提取文件名
    System.IO.Path.GetFileName(fullPath);//文件名

    原文链接:https://blog.csdn.net/fengqingtao2008/article/details/51519002

  • 相关阅读:
    【leetcode】416. Partition Equal Subset Sum
    【leetcode】893. Groups of Special-Equivalent Strings
    【leetcode】892. Surface Area of 3D Shapes
    【leetcode】883. Projection Area of 3D Shapes
    【leetcode】140. Word Break II
    【leetcode】126. Word Ladder II
    【leetcode】44. Wildcard Matching
    【leetcode】336. Palindrome Pairs
    【leetcode】354. Russian Doll Envelopes
    2017.12.22 英语面试手记
  • 原文地址:https://www.cnblogs.com/Dumb-dog/p/12711139.html
Copyright © 2020-2023  润新知