每日一得 - 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.

posted on 2007-03-16 16:35  阿齐  阅读(613)  评论(0编辑  收藏  举报