Skip to main content
.NET Framework Class Library
FileSystemInfo Class

Provides the base class for both FileInfo and DirectoryInfo objects.

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)
Syntax
<[%$TOPIC/975xhcs9_en-us_VS_110_4_0_0_0_0%]> _
<[%$TOPIC/975xhcs9_en-us_VS_110_4_0_0_0_1%](True)> _
<[%$TOPIC/975xhcs9_en-us_VS_110_4_0_0_0_2%]([%$TOPIC/975xhcs9_en-us_VS_110_4_0_0_0_3%].InheritanceDemand, Unrestricted := True)> _
Public MustInherit Class FileSystemInfo _
	Inherits [%$TOPIC/975xhcs9_en-us_VS_110_4_0_0_0_4%] _
	Implements [%$TOPIC/975xhcs9_en-us_VS_110_4_0_0_0_5%]
[[%$TOPIC/975xhcs9_en-us_VS_110_4_0_1_0_0%]]
[[%$TOPIC/975xhcs9_en-us_VS_110_4_0_1_0_1%](true)]
[[%$TOPIC/975xhcs9_en-us_VS_110_4_0_1_0_2%]([%$TOPIC/975xhcs9_en-us_VS_110_4_0_1_0_3%].InheritanceDemand, Unrestricted = true)]
public abstract class FileSystemInfo : [%$TOPIC/975xhcs9_en-us_VS_110_4_0_1_0_4%], 
	[%$TOPIC/975xhcs9_en-us_VS_110_4_0_1_0_5%]
[[%$TOPIC/975xhcs9_en-us_VS_110_4_0_2_0_0%]]
[[%$TOPIC/975xhcs9_en-us_VS_110_4_0_2_0_1%](true)]
[[%$TOPIC/975xhcs9_en-us_VS_110_4_0_2_0_2%]([%$TOPIC/975xhcs9_en-us_VS_110_4_0_2_0_3%]::InheritanceDemand, Unrestricted = true)]
public ref class FileSystemInfo abstract : public [%$TOPIC/975xhcs9_en-us_VS_110_4_0_2_0_4%], 
	[%$TOPIC/975xhcs9_en-us_VS_110_4_0_2_0_5%]
[<[%$TOPIC/975xhcs9_en-us_VS_110_4_0_3_0_0%]>]
[<[%$TOPIC/975xhcs9_en-us_VS_110_4_0_3_0_1%]>]
[<[%$TOPIC/975xhcs9_en-us_VS_110_4_0_3_0_2%](true)>]
[<[%$TOPIC/975xhcs9_en-us_VS_110_4_0_3_0_3%]([%$TOPIC/975xhcs9_en-us_VS_110_4_0_3_0_4%].InheritanceDemand, Unrestricted = true)>]
type FileSystemInfo =  
    class 
        inherit [%$TOPIC/975xhcs9_en-us_VS_110_4_0_3_0_5%] 
        interface [%$TOPIC/975xhcs9_en-us_VS_110_4_0_3_0_6%] 
    end

The FileSystemInfo type exposes the following members.

Constructors
  NameDescription
Protected method Supported by the XNA Framework FileSystemInfoInitializes a new instance of the FileSystemInfo class.
Protected method FileSystemInfo(SerializationInfo, StreamingContext)Initializes a new instance of the FileSystemInfo class with serialized data.
Top
Properties
  NameDescription
Public property Supported by the XNA Framework AttributesGets or sets the attributes for the current file or directory.
Public property Supported by the XNA Framework CreationTimeGets or sets the creation time of the current file or directory.
Public property CreationTimeUtcGets or sets the creation time, in coordinated universal time (UTC), of the current file or directory.
Public property Supported by the XNA Framework ExistsGets a value indicating whether the file or directory exists.
Public property Supported by the XNA Framework ExtensionGets the string representing the extension part of the file.
Public property Supported by the XNA Framework FullNameGets the full path of the directory or file.
Public property Supported by the XNA Framework LastAccessTimeGets or sets the time the current file or directory was last accessed.
Public property LastAccessTimeUtcGets or sets the time, in coordinated universal time (UTC), that the current file or directory was last accessed.
Public property Supported by the XNA Framework LastWriteTimeGets or sets the time when the current file or directory was last written to.
Public property LastWriteTimeUtcGets or sets the time, in coordinated universal time (UTC), when the current file or directory was last written to.
Public property Supported by the XNA Framework NameFor 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.
Top
Methods
  NameDescription
Public method CreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public method Supported by the XNA Framework DeleteDeletes a file or directory.
Public method Supported by the XNA Framework Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected method Supported by the XNA Framework FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Supported by the XNA Framework GetHashCodeServes as the default hash function. (Inherited from Object.)
Public method GetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public method GetObjectDataSets the SerializationInfo object with the file name and additional exception information.
Public method Supported by the XNA Framework GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method InitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected method Supported by the XNA Framework MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected method MemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Public method Supported by the XNA Framework RefreshRefreshes the state of the object.
Public method Supported by the XNA Framework ToStringReturns a string that represents the current object. (Inherited from Object.)
Top
Fields
  NameDescription
Protected field Supported by the XNA Framework FullPathRepresents the fully qualified path of the directory or file.
Protected field Supported by the XNA Framework OriginalPathThe path originally specified by the user, whether relative or absolute.
Top
Remarks

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.

Examples

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
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
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
Version Information

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1
.NET Framework Security
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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