FileSystemInfo 类

定义

FileInfoDirectoryInfo 对象提供基类。Provides the base class for both FileInfo and DirectoryInfo objects.

public ref class FileSystemInfo abstract : MarshalByRefObject, System::Runtime::Serialization::ISerializable
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public abstract class FileSystemInfo : MarshalByRefObject, System.Runtime.Serialization.ISerializable
type FileSystemInfo = class
    inherit MarshalByRefObject
    interface ISerializable
Public MustInherit Class FileSystemInfo
Inherits MarshalByRefObject
Implements ISerializable
继承
FileSystemInfo
派生
属性
实现

示例

下面的示例演示如何遍历所有文件和目录, 并查询有关每个条目的某些信息。The following example shows how to loop through all the files and directories, querying some information about each entry.

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类包含文件和目录操作通用的方法。The FileSystemInfo class contains methods that are common to file and directory manipulation. 对象可以表示文件或目录, 从而充当FileInfoDirectoryInfo对象的基础。 FileSystemInfoA FileSystemInfo object can represent either a file or a directory, thus serving as the basis for FileInfo or DirectoryInfo objects. 分析大量文件和目录时, 请使用此基类。Use this base class when parsing a lot of files and directories.

仅当派生类具有FileSystemInfo FileIOPermissionAccess枚举的 AllAccess 权限时, 派生类才能从继承。A derived class can inherit from FileSystemInfo only if the derived class has the AllAccess permission from the FileIOPermissionAccess enumeration.

在接受路径的成员中, 路径可以引用文件或只引用目录。In members that accept a path, the path can refer to a file or just a directory. 指定的路径还可以引用服务器和共享名称的相对路径或通用命名约定 (UNC) 路径。The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name. 例如, 以下所有内容都是可接受的路径:For example, all the following are acceptable paths:

  • Visual Basic 中的\"\c: \MyDir C#\MyFile.txt" 或 "c:\MyDir\MyFile.txt"。"c:\\MyDir\\MyFile.txt" in C#, or "c:\MyDir\MyFile.txt" in Visual Basic.

  • Visual Basic 中的\"c: C#\MyDir" 或 "c:\MyDir"。"c:\\MyDir" in C#, or "c:\MyDir" in Visual Basic.

  • Visual Basic 中\的 "MyDir C#\MySubdir" 或 "MyDir\MySubDir"。"MyDir\\MySubdir" in C#, or "MyDir\MySubDir" in Visual Basic.

  • Visual Basic\中的 "\\ C#\MyServer\\MyShare" 或 "\MyServer\MyShare"。\"\\\\MyServer\\MyShare" in C#, or "\\MyServer\MyShare" in Visual Basic.

有关常见 i/o 任务的列表, 请参阅常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

构造函数

FileSystemInfo()

初始化 FileSystemInfo 类的新实例。Initializes a new instance of the FileSystemInfo class.

FileSystemInfo(SerializationInfo, StreamingContext)

用序列化数据初始化 FileSystemInfo 类的新实例。Initializes a new instance of the FileSystemInfo class with serialized data.

字段

FullPath

表示目录或文件的完全限定目录。Represents the fully qualified path of the directory or file.

OriginalPath

最初由用户指定的目录(不论是相对目录还是绝对目录)。The path originally specified by the user, whether relative or absolute.

属性

Attributes

获取或设置当前文件或目录的特性。Gets or sets the attributes for the current file or directory.

CreationTime

获取或设置当前文件或目录的创建时间。Gets or sets the creation time of the current file or directory.

CreationTimeUtc

获取或设置当前文件或目录的创建时间,其格式为协调世界时 (UTC)。Gets or sets the creation time, in coordinated universal time (UTC), of the current file or directory.

Exists

获取指示文件或目录是否存在的值。Gets a value indicating whether the file or directory exists.

Extension

获取表示文件扩展名部分的字符串。Gets the string representing the extension part of the file.

FullName

获取目录或文件的完整目录。Gets the full path of the directory or file.

LastAccessTime

获取或设置上次访问当前文件或目录的时间。Gets or sets the time the current file or directory was last accessed.

LastAccessTimeUtc

获取或设置上次访问当前文件或目录的时间,其格式为协调世界时 (UTC)。Gets or sets the time, in coordinated universal time (UTC), that the current file or directory was last accessed.

LastWriteTime

获取或设置上次写入当前文件或目录的时间。Gets or sets the time when the current file or directory was last written to.

LastWriteTimeUtc

获取或设置上次写入当前文件或目录的时间,其格式为协调世界时 (UTC)。Gets or sets the time, in coordinated universal time (UTC), when the current file or directory was last written to.

Name

对于文件,获取该文件的名称。For files, gets the name of the file. 对于目录,如果存在层次结构,则获取层次结构中最后一个目录的名称。For directories, gets the name of the last directory in the hierarchy if a hierarchy exists. 否则,Name 属性获取该目录的名称。Otherwise, the Name property gets the name of the directory.

方法

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(继承自 MarshalByRefObject)
Delete()

删除文件或目录。Deletes a file or directory.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(继承自 MarshalByRefObject)
GetObjectData(SerializationInfo, StreamingContext)

设置带有文件名和附加异常信息的 SerializationInfo 对象。Sets the SerializationInfo object with the file name and additional exception information.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(继承自 MarshalByRefObject)
Refresh()

刷新对象的状态。Refreshes the state of the object.

ToString()

返回原始路径。Returns the original path. 使用 FullNameName 属性作为完整路径或文件/目录名。Use the FullName or Name properties for the full path or file/directory name.

安全性

FileIOPermission
要获取从此类继承的权限。for permission to inherit from this class. 关联的枚举:UnrestrictedAssociated enumeration: Unrestricted 安全操作:继承需求Security Action: Inheritance Demand

适用于

另请参阅