System.IO.FileSystemInfo 类

方法描述

为 FileInfo 和 DirectoryInfo 对象提供基类。

语法定义(C# System.IO.FileSystemInfo 类 的用法)

[SerializableAttribute]
[ComVisibleAttribute(true)]
[FileIOPermissionAttribute(SecurityAction.InheritanceDemand, Unrestricted = true)]
public abstract class FileSystemInfo : MarshalByRefObject, 
	ISerializable

构造函数

构造函数名称 构造函数描述
FileSystemInfo() 初始化 FileSystemInfo 类的新实例。
FileSystemInfo(SerializationInfo, StreamingContext) 用序列化数据初始化 FileSystemInfo 类的新实例。

成员/方法

方法名称 方法描述
CreateObjRef 创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。 (继承自 MarshalByRefObject。)
Delete 删除文件或目录。
Equals(Object) 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)
Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
GetHashCode 用作特定类型的哈希函数。 (继承自 Object。)
GetLifetimeService 检索控制此实例的生存期策略的当前生存期服务对象。 (继承自 MarshalByRefObject。)
GetObjectData 设置带有文件名和附加异常信息的 SerializationInfo 对象。
GetType 获取当前实例的 Type。 (继承自 Object。)
InitializeLifetimeService 获取控制此实例的生存期策略的生存期服务对象。 (继承自 MarshalByRefObject。)
MemberwiseClone() 创建当前 Object 的浅表副本。 (继承自 Object。)
MemberwiseClone(Boolean) 创建当前 MarshalByRefObject 对象的浅表副本。 (继承自 MarshalByRefObject。)
Refresh 刷新对象的状态。
ToString 返回表示当前对象的字符串。 (继承自 Object。)

提示和注释

FileSystemInfo 类包含文件和目录操作所共有的方法。 FileSystemInfo 对象可以表示文件或目录,从而可以作为 FileInfo 或 DirectoryInfo 对象的基础。 当分析许多文件和目录时,请使用该基类。

一个派生类只有从 FileIOPermissionAccess 枚举获得 AllAccess 权限时才能从 FileSystemInfo 继承。

在接受路径的成员中,路径可以是指文件或仅是目录。 指定路径也可以是相对路径或者服务器和共享名称的统一命名约定 (UNC) 路径。 例如,以下都是可接受的路径:

C# 中的“c:\\MyDir\\MyFile.txt”或 Visual Basic 中的“c:\MyDir\MyFile.txt”。

C# 中的“c:\\MyDir”或 Visual Basic 中的“c:\MyDir”。

C# 中的“MyDir\\MySubdir”或 Visual Basic 中的“MyDir\MySubDir”。

C# 中的“\\\\MyServer\\MyShare”或 Visual Basic 中的“\\MyServer\MyShare”。

有关通用 I/O 任务的列表,请参见通用 I/O 任务。

System.IO.FileSystemInfo 类例子

下面的示例演示如何循环浏览所有文件和目录,查询有关每一项的一些信息。

using System;
using System.IO;


namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            //  Loop through all the immediate subdirectories of C.
            foreach (string entry in Directory.GetDirectories(@"C:\"))
            {
                DisplayFileSystemInfoAttributes(new DirectoryInfo(entry));
            }
            //  Loop through all the files in C.
            foreach (string entry in Directory.GetFiles(@"C:\"))
            {
                DisplayFileSystemInfoAttributes(new FileInfo(entry));
            }
        }
        static void DisplayFileSystemInfoAttributes(FileSystemInfo fsi)
        {
            //  Assume that this entry is a file.
            string entryType = "File";

            // Determine if entry is really a directory
            if ((fsi.Attributes & FileAttributes.Directory) == FileAttributes.Directory )
            {
                entryType = "Directory";
            }
            //  Show this entry's type, name, and creation date.
            Console.WriteLine("{0} entry {1} was created on {2:D}", entryType, fsi.FullName, fsi.CreationTime);
        }
    }
}

 // Output will vary based on contents of drive C.

 // Directory entry C:\Documents and Settings was created on Tuesday, November 25, 2003
 // Directory entry C:\Inetpub was created on Monday, January 12, 2004
 // Directory entry C:\Program Files was created on Tuesday, November 25, 2003
 // Directory entry C:\RECYCLER was created on Tuesday, November 25, 2003
 // Directory entry C:\System Volume Information was created on Tuesday, November 2, 2003
 // Directory entry C:\WINDOWS was created on Tuesday, November 25, 2003
 // File entry C:\IO.SYS was created on Tuesday, November 25, 2003
 // File entry C:\MSDOS.SYS was created on Tuesday, November 25, 2003
 // File entry C:\pagefile.sys was created on Saturday, December 27, 2003

继承层次结构

System.Object

System.MarshalByRefObject

System.IO.FileSystemInfo

System.IO.DirectoryInfo

System.IO.FileInfo

命名空间

namespace: System.IO

程序集: mscorlib(在 mscorlib.dll 中)

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。