C#的FileInfo类实现文件操作实例
作者:shichen2014 时间:2021-06-30 06:39:44
C#的FileInfo类提供了与File类相同的功能,不同的是FileInfo提供的都是成员方法,使用示例如下所示:
1、读文件:
//创建只读 System.IO.FileStream。
public System.IO.FileStream OpenRead()
//创建使用 UTF8 编码、从现有文本文件中进行读取的 System.IO.StreamReader。
public System.IO.StreamReader OpenText()
2、写文件:
//创建只写 System.IO.FileStream。
public System.IO.FileStream OpenWrite()
3、追加内容:
//创建一个 System.IO.StreamWriter,它向 System.IO.FileInfo 的此实例表示的文件追加文本。
public System.IO.StreamWriter AppendText()
4、打开文件:
//在指定的模式中打开文件。
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 Open(System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share)
5、复制、移动、替换:
//将现有文件复制到新文件,不允许覆盖现有文件。
public System.IO.FileInfo CopyTo(string destFileName)
//将现有文件复制到新文件,允许覆盖现有文件。
public System.IO.FileInfo CopyTo(string destFileName, bool overwrite)
//将指定文件移到新位置,并提供指定新文件名的选项。
public void MoveTo(string destFileName)
//使用当前 System.IO.FileInfo 对象所描述的文件替换指定文件的内容,这一过程将删除原始文件,并创建被替换文件的备份。
public System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName)
//使用当前 System.IO.FileInfo 对象所描述的文件替换指定文件的内容,这一过程将删除原始文件,并创建被替换文件的备份。还指定是否忽略合并错误。
public System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors)
6、加密解密、删除:
//将某个文件加密,使得只有加密该文件的帐户才能将其解密。
public void Encrypt()
//解密由当前帐户使用 System.IO.FileInfo.Encrypt() 方法加密的文件。
public void Decrypt()
//永久删除文件。
public override void Delete()
7、获得文件属性:
//获取父目录的实例。
public System.IO.DirectoryInfo Directory { get; }
//获取表示目录的完整路径的字符串。
public string DirectoryName { get; }
//获取指示文件是否存在的值。
public override bool Exists { get; }
//获取或设置确定当前文件是否为只读的值。
public bool IsReadOnly { set; get; }
//获取当前文件的大小(字节)。
public long Length { get; }
//获取文件名。
public override string Name { get; }
在FileInfo中获取文件的相关属性不再是方法了,都是通过属性获得的,并且除是否只读属性为可读可写的,其他属性都是只读的。
总结:
大家注意到,我们在FileInfo中提供的方法不再是静态的,并且返回值都是FileStream类型的,是一个文件流,因此我们在使用FileInfo这个类时还需要结合FileStream类一起使用。而在介绍File类时,所有的操作都是通过静态方法实现的,并且返回值都是具体的值类型。
这也算是对File类与FileInfo类的粗略的对比。
标签:C#,文件,操作
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java String类和StringBuffer类的区别介绍
2022-06-12 11:42:13
C#使用XML序列化操作菜单的方法
2022-08-03 08:54:34
Android巧用DecorView实现对话框功能
2022-07-14 14:03:40
![](https://img.aspxhome.com/file/2023/8/91658_0s.gif)
详解Alibaba Java诊断工具Arthas查看Dubbo动态代理类
2021-08-04 03:16:28
![](https://img.aspxhome.com/file/2023/4/64064_0s.png)
MyBatis动态SQL标签的用法详解
2021-07-24 10:38:56
java判断三位数的实例讲解
2023-07-06 08:29:53
![](https://img.aspxhome.com/file/2023/9/86409_0s.png)
Maven学习----Maven安装与环境变量配置教程
2021-12-04 08:20:25
![](https://img.aspxhome.com/file/2023/8/60678_0s.png)
Logback 使用TurboFilter实现日志级别等内容的动态修改操作
2022-06-10 04:17:53
Java 梳理总结关于static关键字常见问题
2021-12-11 11:49:01
一篇超详细的Spring Boot整合Mybatis文章
2022-01-27 10:02:58
![](https://img.aspxhome.com/file/2023/3/72313_0s.jpg)
idea使用外置tomcat配置springboot详细步骤
2023-11-20 03:56:49
![](https://img.aspxhome.com/file/2023/8/98108_0s.png)
C#有效防止同一账号多次登录(附三种方法)
2023-05-23 10:32:45
图文并茂讲解RocketMQ消息类别
2023-06-11 07:59:41
![](https://img.aspxhome.com/file/2023/0/63160_0s.png)
详解JS与APP原生控件交互
2022-11-21 21:28:15
![](https://img.aspxhome.com/file/2023/3/108303_0s.png)
Java Socket 编程详解
2022-06-13 12:06:57
Java中websocket消息推送的实现代码
2023-06-02 09:26:56
JavaAPI的使用方法详解
2022-10-30 23:00:40
![](https://img.aspxhome.com/file/2023/5/105945_0s.png)
AndroidStduio3.0 使用gradle将module打包jar文件的方法
2023-07-01 06:57:20
![](https://img.aspxhome.com/file/2023/4/84224_0s.png)
Java集合之Map接口的实现类精解
2023-10-07 15:10:37
![](https://img.aspxhome.com/file/2023/3/63653_0s.png)
C#装箱与拆箱操作的深入讲解
2023-04-29 19:10:06
![](https://img.aspxhome.com/file/2023/2/89732_0s.png)