FileSystemInfo 类

定义

FileInfoDirectoryInfo 对象提供基类。

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 namespace System;
using namespace System::IO;


namespace ConsoleApplication2
{
    public ref class Program
    {
    public:
        static void Main()
        {
            //  Loop through all the immediate subdirectories of C.
            for each (String^ entry in Directory::GetDirectories("C:\\"))
            {
                DisplayFileSystemInfoAttributes(gcnew DirectoryInfo(entry));
            }
            //  Loop through all the files in C.
            for each (String^ entry in Directory::GetFiles("C:\\"))
            {
                DisplayFileSystemInfoAttributes(gcnew 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);
        }
    };
};

int main()
{
    ConsoleApplication2::Program::Main();
}

 // 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
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) 路径。 例如,以下所有路径都是可接受的路径:

  • 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 任务

构造函数

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()

返回原始路径。 使用 FullNameName 属性作为完整路径或文件/目录名。

ToString()

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

(继承自 Object)

适用于

另请参阅