FileSystemInfo Class

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Provides the base class for both FileInfo and DirectoryInfo objects.

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)

System.Object
  System.MarshalByRefObject
    System.IO.FileSystemInfo
      System.IO.DirectoryInfo
      System.IO.FileInfo

<SerializableAttribute>
<ComVisibleAttribute(True)>
<FileIOPermissionAttribute(SecurityAction.InheritanceDemand, Unrestricted := True)>
Public MustInherit Class FileSystemInfo
	Inherits MarshalByRefObject
	Implements ISerializable

NameDescription
System_CAPS_protmethodFileSystemInfo()

Initializes a new instance of the FileSystemInfo class.

System_CAPS_protmethodFileSystemInfo(SerializationInfo, StreamingContext)

Initializes a new instance of the FileSystemInfo class with serialized data.

NameDescription
System_CAPS_pubpropertyAttributes

Gets or sets the attributes for the current file or directory.

System_CAPS_pubpropertyCreationTime

Gets or sets the creation time of the current file or directory.

System_CAPS_pubpropertyCreationTimeUtc

Gets or sets the creation time, in coordinated universal time (UTC), of the current file or directory.

System_CAPS_pubpropertyExists

Gets a value indicating whether the file or directory exists.

System_CAPS_pubpropertyExtension

Gets the string representing the extension part of the file.

System_CAPS_pubpropertyFullName

Gets the full path of the directory or file.

System_CAPS_pubpropertyLastAccessTime

Gets or sets the time the current file or directory was last accessed.

System_CAPS_pubpropertyLastAccessTimeUtc

Gets or sets the time, in coordinated universal time (UTC), that the current file or directory was last accessed.

System_CAPS_pubpropertyLastWriteTime

Gets or sets the time when the current file or directory was last written to.

System_CAPS_pubpropertyLastWriteTimeUtc

Gets or sets the time, in coordinated universal time (UTC), when the current file or directory was last written to.

System_CAPS_pubpropertyName

For files, gets the name of the file. For directories, gets the name of the last directory in the hierarchy if a hierarchy exists. Otherwise, the Name property gets the name of the directory.

NameDescription
System_CAPS_pubmethodCreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodDelete()

Deletes a file or directory.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodGetObjectData(SerializationInfo, StreamingContext)

Sets the SerializationInfo object with the file name and additional exception information.

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodInitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodRefresh()

Refreshes the state of the object.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_protfieldFullPath

Represents the fully qualified path of the directory or file.

System_CAPS_protfieldOriginalPath

The path originally specified by the user, whether relative or absolute.

The FileSystemInfo class contains methods that are common to file and directory manipulation. A 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.

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. 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:

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

  • "c:\\MyDir" in C#, or "c:\MyDir" in Visual Basic.

  • "MyDir\\MySubdir" in C#, or "MyDir\MySubDir" in Visual Basic.

  • "\\\\MyServer\\MyShare" in C#, or "\\MyServer\MyShare" in Visual Basic.

For a list of common I/O tasks, see Common I-O Tasks.

The following example shows how to loop through all the files and directories, querying some information about each entry.

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

FileIOPermission

for permission to inherit from this class. Associated enumeration: Unrestricted

Security Action: Inheritance Demand

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: