每日一得 - System.IO.FileInfo

MSDN介绍,FileInfo类的存在原因为“提供一系列诸如创建、拷贝、删除、移动和打开文件等实例方法,且为创建文件流(FileStream)对象提供辅助功能”(1)。还特别强调了一下:如果你需要多次操作一个文件,那么使用一个FileInfo实例比使用File提供的那些个静态方法更有效率。

昨天使用了FileInfo的一个很常见的功能,即去掉某个文件的只读属性--此功能是通过FileInfo实例的FileAttributes属性实现的:

public void RemoveReadOnlyAttr(string fileName)
{
FileInfo file = new FileInfo(fileName);
if ((FileAttributes.ReadOnly & file.Attributes) == FileAttributes.ReadOnly)
{
file.Attributes = file.Attributes & (~FileAttributes.ReadOnly);
}
}

引用MSDN内容

(1)原文:Provides instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects

(2)原文:If you are going to reuse an object several times, consider using the instance method of FileInfo instead of the corresponding static methods of the File class, because a security check will not always be necessary.

转载于:https://www.cnblogs.com/westsource/archive/2007/03/16/daily_FileInfo.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值