当前位置: 移动技术网 > IT编程>开发语言>c# > C# 基础知识系列- 14 IO篇 文件的操作

C# 基础知识系列- 14 IO篇 文件的操作

2020年05月01日  | 移动技术网IT编程  | 我要评论

@

0. 前言

本章节是io篇的第二集,我们在上一篇中介绍了c#中io的基本概念和一些基本方法,接下来我们介绍一下操作文件的方法。在编程的世界中,操作文件是一个很重要的技能。

1. 文件、目录和路径

在开始操作之前,先大概讲解一下基本概念。在计算机系统中,文件是以硬盘为载体存储在计算机上的信息集合。文件通常会有一个后缀名,表示文件格式(当然,通常的另一个含义就是可能没有)。我们最常见到的图片文件,后缀有jpg/png/gif这些常见的;文本文件为txt等。

目录,不严谨的来讲可以用文件夹代替。不过严格来说,目录指的是文件所在的文件夹以及文件夹的位置这些信息的集合。

路径是指文件或文件夹所在的位置的字符串表示,有相对路径和绝对路径,有物理路径和网络路径等一系列这些划分。

  • 相对路径指的是,相对程序所在目录目标文件所在的目录路径
  • 绝对路径指的是从系统或者网站的目录起点开始文件所在的位置,也就是说无论程序在哪都能通过绝对路径访问到对应文件
  • 物理路径是指文件在磁盘的路径,划分依据与之前的两种并不一致,所以不是并列关系
  • 网络路径是指网络或文件是在网络服务上部署的,通过uri访问的路径信息

好了,基本概念介绍到这里,让我们来看看如何实现c#操作文件吧。

1.1 file和fileinfo

c# 提供了两个访问文件的入口,file和fileinfo这两个类。有人可能要迷惑了,为啥要提供两个呢,这两个又有啥子不一样的呢?别急,让我们来一起看一看吧。

我们先来观察一下两个类的声明方式有什么不一样的:

public static class file;
public sealed class fileinfo : system.io.filesysteminfo;

我们忽略突然冒出来的filesysteminfo,只需要明白它是fileinfo的基类即可。

通过两个类的声明方式,可以看出file是一个工具类,而fileinfo则是文件对象。所以,file更多的用在快速操作文件并不需要长时间多次使用同一个文件的场景,而fileinfo则适合同一个文件的多次使用。

1.1.1 file工具类

我们先来看下file支持哪些操作:

a.文件读取

public static byte[] readallbytes (string path);
public static string[] readalllines (string path);
public static string[] readalllines (string path, system.text.encoding encoding);
public static string readalltext (string path);
public static string readalltext (string path, system.text.encoding encoding);
public static system.collections.generic.ienumerable<string> readlines (string path);

先从名称上分析方法应该是什么,应该具有哪些功能?

  • readallbytes以二进制的形式一次性把文件全部读出来
  • readalllines打开文本文件,将文件内容一行一行的全部读出来并返回
  • readalltext打开文件,并将文件所有内容一次性读出来
  • readlines 这是一个新的方法,根据返回值和方法名称,可以判断它应该与readalllines有着类似的行为

readlines和readalllines的区别:

  • readalllines返回的是字符串数组,所以该方法会一次性将文件内容全部读出
  • readlines返回的是一个可枚举对象,根据之前在linq系列和集合系列的知识,我们能判断出,这个方法不会立即返回数据

所以我们很轻易的就能得出,readalllines不会过久的持有文件对象,但是不适合操作大文件;readlines对于大文件的操作更擅长一些,但是可能会更久的持有文件

b.写入文件

public static void appendalllines (string path, system.collections.generic.ienumerable<string> contents);
public static void appendalllines (string path, system.collections.generic.ienumerable<string> contents, system.text.encoding encoding);
public static void appendalltext (string path, string contents);
public static void appendalltext (string path, string contents, system.text.encoding encoding);
public static void writeallbytes (string path, byte[] bytes);
public static void writealllines (string path, string[] contents, system.text.encoding encoding);
public static void writealltext (string path, string contents);
public static void writealltext (string path, string contents, system.text.encoding encoding);

来,我们简单看一下这几个方法具体作用:

  • appendalllines:追加行到文件末尾
  • appendalltext :将字符串内容追加到文件末尾
  • writebytes:将字节数组写到文件里,如果文件有内容就覆盖原有内容
  • writealllines:按行写入文件中,如果文件有内容则覆盖原有内容
  • writealltext:将内容写入文件,如果文件有内容则覆盖原有内容

在使用file写入文件的时候,如果文件不存在则会自动创建文件。

c. 复制文件

file类提供了简单易用的复制文件功能,只需要指定源文件和新文件即可:

public static void copy (string sourcefilename, string destfilename);
public static void copy (string sourcefilename, string destfilename, bool overwrite);

这两个方法对的作用就是将 sourcefilename复制为destfilename。第一个方法不允许复制为已存在的文件,也就是说如果destfilename已存在则报错。第二个方法则通过overwrite指定是否覆盖。

d.移动文件

与复制文件相同的使用方式,file提供了移动文件的方法:

public static void move (string sourcefilename, string destfilename);
public static void move (string sourcefilename, string destfilename, bool overwrite);

注意事项与复制文件一致。

e.删除文件

public static void delete (string path);

1.1.2 fileinfo 对象类

fileinfo提供了文件的创建、复制、删除、移动和打开等属性和实例方法。我们先来看看,如果创建一个fileinfo:

public fileinfo (string filename);

通过指定文件路径,来换取一个fileinfo对象,如果filename指定的是目录则会提示错误。

好,现在我们已经可以获取一个fileinfo对象实例了,那么一起来看看fileinfo支持哪些内容吧:

a. 先来看看文件的基本属性

public override bool exists { get; }

文件是否存在,等效于file.existss(string path)。

public string directoryname { get; }

获取文件所在目录的完整路径(绝对路径)。

public system.io.directoryinfo directory { get; }

获取文件所在目录的目录类型实例。

public long length { get; }

获取文件的大小,单位是字节。

public override string name { get; }

获取文件名,包括文件的扩展名。

b. 文件的操作

对于fileinfo实例来说,对于文件的操作大多都是基于流来完成的(这部分请留意下一篇内容),这里先看一下它的实例方法:

public system.io.streamwriter appendtext ();//创建一个流适配器,在适配器里追加文本到文件中
public system.io.fileinfo copyto (string destfilename);//将现有文件复制到新文件,并返回新文件的实例,不支持覆盖
public system.io.fileinfo copyto (string destfilename, bool overwrite);//根据orverwrite确定是否覆盖
public system.io.filestream create ();//创建当前对象代表的文件,并返回一个文件流
public system.io.streamwriter createtext ();//与appendtext类似,但会覆盖文件原有内容
public override void delete ();//删除文件
public void moveto (string destfilename);// 将文件移动到新文件,不支持覆盖已存在文件
public void moveto (string destfilename, bool overwrite);// 根据overwrite确定是否覆盖
public system.io.filestream open (system.io.filemode mode);// 根据模式打开文件
public system.io.filestream open (system.io.filemode mode, system.io.fileaccess access);//指定权限和模式,打开文件
public system.io.filestream openread ();//打开一个只能读取的文件流
public system.io.streamreader opentext ();//打开一个读流适配器
public system.io.filestream openwrite ();// 打开一个只能写的流

最新版c#的api,取消了通过fileinfo获取文件的格式名的属性以及其他的很多属性,只保留了文中提到的几个属性。

1.2 directory和directoryinfo

与之前的类似,directory也是个工具类,directoryinfo则代表目录实例。

1.2.1 directory

先来个简单的:

a. 创建目录:

public static system.io.directoryinfo createdirectory (string path);

如果目录已存在,则跳过创建,直接返回指定路径的directoryinfo实例

b.是否存在:

public static bool exists (string path);

返回是否存在这个目录。

c.返回目录下的所有文件

public static string[] getfiles (string path);

d. 返回目录下的所有子目录:

public static string[] getdirectories (string path);
public static string[] getdirectories (string path, string searchpattern);
public static string[] getdirectories (string path, string searchpattern, system.io.enumerationoptions enumerationoptions);
public static string[] getdirectories (string path, string searchpattern, system.io.searchoption searchoption);

除了上文提到的 getdirectories 方法可以直接返回目录下所有子目录以外,还有一组方法也可以枚举出当前目录下的子目录:

public static system.collections.generic.ienumerable<string> enumeratedirectories (string path);

枚举 path 目录下的所有子目录。

public static system.collections.generic.ienumerable<string> enumeratedirectories (string path, string searchpattern);

searchpattern,搜索名称字符串,可以包含有效文本路径和通配符(* 和 ?)的组合,但不支持正则表达式。

public static system.collections.generic.ienumerable<string> enumeratedirectories (string path, string searchpattern, system.io.enumerationoptions enumerationoptions);
public static system.collections.generic.ienumerable<string> enumeratedirectories (string path, string searchpattern, system.io.searchoption searchoption);

这两个方法放在一起讲,这两个是对上一个方法的增强和补充。其中 enumerationoptions 是类,可以配置查询的条件;searchoption 是个枚举,选择只查询当前目录的子目录名称还是继续深入查询子孙目录。

e.查看目录下的所有文件-补充

与子目录查询相同,directory也支持这么几组查询方法:

public static string[] getfiles (string path);
public static string[] getfiles (string path, string searchpattern);
public static string[] getfiles (string path, string searchpattern, system.io.enumerationoptions enumerationoptions);
public static string[] getfiles (string path, string searchpattern, system.io.searchoption searchoption);

从参数上看,可以看出来这是返回子目录下的文件列表。其中使用 searchpattern查询名称,enumerationoptions 作为查询条件,searchoption 作为查询的深度。

同样,查询文件也可以使用枚举方法:

public static system.collections.generic.ienumerable<string> enumeratefiles (string path);
public static system.collections.generic.ienumerable<string> enumeratefiles (string path, string searchpattern);
public static system.collections.generic.ienumerable<string> enumeratefiles (string path, string searchpattern, system.io.enumerationoptions enumerationoptions);
public static system.collections.generic.ienumerable<string> enumeratefiles (string path, string searchpattern, system.io.searchoption searchoption);

f.获取当前目录

public static string getcurrentdirectory ();

在程序中调用这个方法可以获取程序执行时的目录,如果是在调试阶段,目录是指程序的主方法所在目录;如果在发布之后,也就是运行阶段,该目录指程序所在目录。

g.获取上级目录

public static system.io.directoryinfo getparent (string path);

获取传入目录的上级目录信息。

h.目录移动

public static void move (string sourcedirname, string destdirname);

sourcedirname 移动到 destdirname,其中destdirname所代表的目录不能纯在。这个方法有个很有意思的特点,它也支持移动文件。也就是说,如果sourcedirnanme指向的是一个文件,那么destdirname也必须是一个文件类型的路径字符串。

i.删除目录

public static void delete (string path);//删除 path所代表的目录,如果目录非空则提示无法删除
public static void delete (string path, bool recursive);// recursive指示是否同时删除子目录和文件

以上是directory类的一些常用方法,当然还有更多的内容留待小伙伴一起发掘。传送门==>

1.2.2 directoryinfo

之前的篇幅我们介绍了directory的工具类所支持的方法,接下来我们看一下 directoryinfo有哪些属性和方法吧。

public directoryinfo (string path);

初始化的方式很简单,直接传递一个目录的路径字符串,就可以获取一个目录信息类了。

接下来看看,directoryinfo支持的属性:

public override bool exists { get; }// 目录是否存在
public override string name { get; }// 目录名称,不是路径
public system.io.directoryinfo parent { get; }//如果有上级目录,则返回上级目录,如果没有则返回 null
public system.io.directoryinfo root { get; }//获取目录的根目录

我们路过了directoryinfo的属性,看到了它一部分特点,那么我们该怎么使用呢?

public void create ();

创建目录信息所代表的目录,如果目录已存在,则不会有任何变化 。如果这个目录的父目录也不存在,则自动创建父目录

public system.io.directoryinfo createsubdirectory (string path);

创建 pathi指定的子目录。

public override void delete ();

如果当前目录是空目录,调用可直接删除,如果非空则会提示错误。

public void delete (bool recursive);

根据参数 recursive指定是否删除当前目录的子目录。

public system.io.directoryinfo[] getdirectories ();
public system.io.directoryinfo[] getdirectories (string searchpattern);
public system.io.directoryinfo[] getdirectories (string searchpattern, system.io.enumerationoptions enumerationoptions);
public system.io.directoryinfo[] getdirectories (string searchpattern, system.io.searchoption searchoption);

获取子目录的数组,参数与 directory 的同名方法一致。

public system.collections.generic.ienumerable<system.io.directoryinfo> enumeratedirectories ();
public system.collections.generic.ienumerable<system.io.directoryinfo> enumeratedirectories (string searchpattern);
public system.collections.generic.ienumerable<system.io.directoryinfo> enumeratedirectories (string searchpattern, system.io.enumerationoptions enumerationoptions);
public system.collections.generic.ienumerable<system.io.directoryinfo> enumeratedirectories (string searchpattern, system.io.searchoption searchoption);

返回一个子目录信息的可枚举集合。

public system.io.fileinfo[] getfiles ();
public system.io.fileinfo[] getfiles (string searchpattern);
public system.io.fileinfo[] getfiles (string searchpattern, system.io.enumerationoptions enumerationoptions);
public system.io.fileinfo[] getfiles (string searchpattern, system.io.searchoption searchoption);

嗯,依旧类似的写法,获取文件信息的数组

public system.collections.generic.ienumerable<system.io.fileinfo> enumeratefiles ();
public system.collections.generic.ienumerable<system.io.fileinfo> enumeratefiles (string searchpattern);
public system.collections.generic.ienumerable<system.io.fileinfo> enumeratefiles (string searchpattern, system.io.enumerationoptions enumerationoptions);
public system.collections.generic.ienumerable<system.io.fileinfo> enumeratefiles (string searchpattern, system.io.searchoption searchoption);

返回文件的可枚举集合。

public void moveto (string destdirname);

把当前目录移动到对应的目录。

依旧未完待续,下一篇将为大家介绍一下 path类和fileinfo与directoryinfo的父类 filesysteminfo 这两个类的api,然后演示一下如何使用流来读写文件。在文件和目录这块内容里,我故意忽略了权限的介绍,这部分我将会放在进阶篇中介绍。

api的介绍总是这么枯燥乏味,不过请期待一下,在io篇完成后,我会演示一下如何做一个简单的文件查找工具。

简单介绍一下这个工具的内容:它会遍历系统里所有文件的路径信息,然后记录到一个缓存文件中,用户输入一个要查询的文件名时,我们可以通过读取缓存文件确认文件所在目录。

更多内容烦请关注

file

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网