FileSystemInfo Class
Assembly: mscorlib (in mscorlib.dll)
'Declaration <SerializableAttribute> _ <ComVisibleAttribute(True)> _ Public MustInherit Class FileSystemInfo Inherits MarshalByRefObject Implements ISerializable 'Usage Dim instance As FileSystemInfo
/** @attribute SerializableAttribute() */ /** @attribute ComVisibleAttribute(true) */ public abstract class FileSystemInfo extends MarshalByRefObject implements ISerializable
SerializableAttribute ComVisibleAttribute(true) public abstract class FileSystemInfo extends MarshalByRefObject implements ISerializable
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.
When first called, FileSystemInfo calls Refresh and returns the cached information on APIs to get attributes and so on. On subsequent calls, you must call Refresh to get the latest copy of the information.
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.
The following table lists examples of other typical or related I/O tasks.
| To do this... | See the example in this topic... |
|---|---|
| Create a text file. | |
| Write to a text file. | |
| Read from a text file. | |
| Append text to a file. | |
| Delete a file. | |
| Rename or move a file. | |
| Copy a file. | |
| Create a directory. | System.IO.DirectoryInfo |
| Create a subdirectory. | |
| Rename or move a directory. | |
| Delete a directory. | |
| Copy a directory. | |
| Sort files in a directory by size. | |
| See the files in a directory. | |
| See the subdirectories of a directory. | |
| See all the files in all subdirectories of a directory. | GetFileSystemInfos |
| Find the size of a directory. | Directory |
| Get the size of a file. | |
| Get the attributes of a file. | |
| Set the attributes of a file. | |
| Determine if a file exists. | |
| Read from a binary file. | |
| Write to a binary file. | |
| Retrieve a file extension. | |
| Retrieve the fully qualified path of a file. | |
| Retrieve the file name and extension from a path. | |
| Change the extension of a file. |
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) <> 0 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 Associated enumeration: Unrestricted Security Action: Inheritance Demand For permission to inherit from this class.
System.MarshalByRefObject
System.IO.FileSystemInfo
System.IO.DirectoryInfo
System.IO.FileInfo
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.