FileSystemInfo 类

定义

为这两个 FileInfo 对象和 DirectoryInfo 对象提供基类。

public ref class FileSystemInfo abstract
public ref class FileSystemInfo abstract : MarshalByRefObject, System::Runtime::Serialization::ISerializable
public abstract class FileSystemInfo
public abstract class FileSystemInfo : MarshalByRefObject, System.Runtime.Serialization.ISerializable
[System.Serializable]
public abstract class FileSystemInfo : MarshalByRefObject, System.Runtime.Serialization.ISerializable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class FileSystemInfo : MarshalByRefObject, System.Runtime.Serialization.ISerializable
type FileSystemInfo = class
type FileSystemInfo = class
    inherit MarshalByRefObject
    interface ISerializable
[<System.Serializable>]
type FileSystemInfo = class
    inherit MarshalByRefObject
    interface ISerializable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type FileSystemInfo = class
    inherit MarshalByRefObject
    interface ISerializable
Public MustInherit Class FileSystemInfo
Public MustInherit Class FileSystemInfo
Inherits MarshalByRefObject
Implements ISerializable
继承
FileSystemInfo
继承
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
Imports System.IO
Module Module1

    Sub Main()
        ' Loop through all the immediate subdirectories of C.
        For Each entry As String In Directory.GetDirectories("C:\")
            DisplayFileSystemInfoAttributes(New DirectoryInfo(entry))
        Next

        ' Loop through all the files in C.
        For Each entry As String In Directory.GetFiles("C:\")
            DisplayFileSystemInfoAttributes(New FileInfo(entry))
        Next
    End Sub

    Sub DisplayFileSystemInfoAttributes(ByVal fsi As IO.FileSystemInfo)
        ' Assume that this entry is a file.
        Dim entryType As String = "File"

        ' Determine if this entry is really a directory.
        If (fsi.Attributes And FileAttributes.Directory) = FileAttributes.Directory Then
            entryType = "Directory"
        End If

        ' Show this entry's type, name, and creation date.
        Console.WriteLine("{0} entry {1} was created on {2:D}", _
            entryType, fsi.FullName, fsi.CreationTime)
    End Sub
End Module

' 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

注解

FileSystemInfo 类包含文件和目录操作通用的方法。 对象FileSystemInfo可以表示文件或目录,从而充当或DirectoryInfo对象的基础FileInfo。 分析大量文件和目录时,请使用此基类。

仅当派生类具有枚举的 AllAccess 权限时,派生类才能继承FileSystemInfoFileIOPermissionAccess

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

  • Visual Basic 中的“c:\\MyDir\\MyFile.txt”,或“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 任务

构造函数

名称 说明
FileSystemInfo()

初始化 FileSystemInfo 类的新实例。

FileSystemInfo(SerializationInfo, StreamingContext)
已过时.

使用序列化的数据初始化 FileSystemInfo 类的新实例。

字段

名称 说明
FullPath

表示目录或文件的完全限定路径。

OriginalPath

最初由用户指定的路径,无论是相对路径还是绝对路径。

属性

名称 说明
Attributes

获取或设置当前文件或目录的属性。

CreationTime

获取或设置当前文件或目录的创建时间。

CreationTimeUtc

获取或设置当前文件或目录的协调世界时(UTC)的创建时间。

Exists

获取一个值,该值指示文件或目录是否存在。

Extension

获取文件名的扩展名部分,包括前导点 . (即使它是整个文件名)或空字符串(如果没有扩展名)。

FullName

获取目录或文件的完整路径。

LastAccessTime

获取或设置上次访问当前文件或目录的时间。

LastAccessTimeUtc

获取或设置上次访问当前文件或目录的时间(UTC)。

LastWriteTime

获取或设置上次写入当前文件或目录的时间。

LastWriteTimeUtc

获取或设置上次写入当前文件或目录的时间(UTC)。

LinkTarget

获取位于其中 FullName的链接的目标路径,或者 nullFileSystemInfo 实例不表示链接。

Name

对于文件,获取文件的名称。 对于目录,获取层次结构中最后一个目录的名称(如果存在层次结构)。 否则,该 Name 属性将获取目录的名称。

UnixFileMode

获取或设置当前文件或目录的 Unix 文件模式。

方法

名称 说明
CreateAsSymbolicLink(String)

创建指向 FullName 指定 pathToTarget位置的符号链接。

CreateObjRef(Type)

创建一个对象,其中包含生成用于与远程对象通信的代理所需的所有相关信息。

(继承自 MarshalByRefObject)
Delete()

删除文件或目录。

Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetLifetimeService()
已过时.

检索控制此实例的生存期策略的当前生存期服务对象。

(继承自 MarshalByRefObject)
GetObjectData(SerializationInfo, StreamingContext)
已过时.

SerializationInfo使用文件名和其他异常信息设置对象。

GetType()

获取当前实例的 Type

(继承自 Object)
InitializeLifetimeService()
已过时.

获取生存期服务对象来控制此实例的生存期策略。

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。

(继承自 MarshalByRefObject)
Refresh()

刷新对象的状态。

ResolveLinkTarget(Boolean)

获取指定链接的目标。

ToString()

返回原始路径。 使用 FullName 完整路径或文件/目录名称的或 Name 属性。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅