FileInfo Class

FileInfo Class

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Provides instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects. This class cannot be inherited.

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

public sealed class FileInfo : FileSystemInfo

The FileInfo type exposes the following members.

Public methodFileInfoInitializes a new instance of the FileInfo class, which acts as a wrapper for a file path.

Public propertyAttributesGets or sets the FileAttributes of the current FileSystemInfo. (Inherited from FileSystemInfo.)
Public propertyCreationTimeGets or sets the creation time of the current FileSystemInfo object. (Inherited from FileSystemInfo.)
Public propertyDirectoryGets an instance of the parent directory.
Public propertyDirectoryNameGets a string representing the directory's full path.
Public propertyExistsGets a value indicating whether a file exists. (Overrides FileSystemInfo.Exists.)
Public propertyExtensionGets the string representing the extension part of the file. (Inherited from FileSystemInfo.)
Public propertyFullNameGets the full path of the directory or file. (Inherited from FileSystemInfo.)
Public propertyLastAccessTimeGets or sets the time the current file or directory was last accessed. (Inherited from FileSystemInfo.)
Public propertyLastWriteTimeGets or sets the time when the current file or directory was last written to. (Inherited from FileSystemInfo.)
Public propertyLengthGets the size, in bytes, of the current file.
Public propertyNameGets the name of the file. (Overrides FileSystemInfo.Name.)

Public methodAppendTextCreates a StreamWriter that appends text to the file represented by this instance of the FileInfo.
Public methodCopyTo(String)Copies an existing file to a new file, disallowing the overwriting of an existing file.
Public methodCopyTo(String, Boolean)Copies an existing file to a new file, allowing the overwriting of an existing file.
Public methodCreateCreates a file.
Public methodCreateTextCreates a StreamWriter that writes a new text file.
Public methodDeletePermanently deletes a file. (Overrides FileSystemInfo.Delete().)
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodMoveToMoves a specified file to a new location, providing the option to specify a new file name.
Public methodOpen(FileMode)Opens a file in the specified mode.
Public methodOpen(FileMode, FileAccess)Opens a file in the specified mode with read, write, or read/write access.
Public methodOpen(FileMode, FileAccess, FileShare)Opens a file in the specified mode with read, write, or read/write access and the specified sharing option.
Public methodOpenReadCreates a read-only FileStream.
Public methodOpenTextCreates a StreamReader with UTF8 encoding that reads from an existing text file.
Public methodOpenWriteCreates a write-only FileStream.
Public methodRefreshRefreshes the state of the object. (Inherited from FileSystemInfo.)
Public methodToStringReturns the path as a string. (Overrides Object.ToString().)

Protected fieldFullPathInfrastructure. Represents the fully qualified path of the directory or file. (Inherited from FileSystemInfo.)
Protected fieldOriginalPathInfrastructure. The path originally specified by the user, whether relative or absolute. (Inherited from FileSystemInfo.)

Use the FileInfo class for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to files.

Many of the FileInfo methods return other I/O types when you create or open files. You can use these other types to further manipulate a file. For more information, see specific FileInfo members such as Open, OpenRead, OpenText, CreateText, or Create.

If you are going to reuse an object several times, consider using the instance method of FileInfo instead of the corresponding static methods of the File class, because a security check will not always be necessary.


In members that accept a path as an input string, that path must be well-formed or an exception is raised. For example, if a path is fully qualified but begins with a space, the path is not trimmed in methods of the class. Therefore, the path is malformed and an exception is raised. Similarly, a path or a combination of paths cannot be fully qualified twice. For example, "c:\temp c:\windows" also raises an exception in most cases. Ensure that your paths are well-formed when using methods that accept a path string.

In members that accept a path, the path can refer to a file or just a directory.

The FileInfo class provides the following properties that enable you to retrieve information about a file. For an example of how to use each property, see the property pages.

  • The Directory property retrieves an object that represents the parent directory of a file.

  • The DirectoryName property retrieves the full path of the parent directory of a file.

  • The Exists property checks for the presence of a file before operating on it.

  • The Length retrieves the size of a file.

  • The Name retrieves the name of a file.

The following example displays an image picked at random from the user's My Pictures folder. It uses the DirectoryInfo class to obtain an enumerable collection of FileInfo objects that represent files that have a .jpg or .png extension. That collection is used to construct a List<T> collection so that its index can be used to select a file that corresponds to the random number.

The example then creates a bitmap image by using the FileStream class and sets it as the source for an Image control (named myImage).

private void LoadImage()

	DirectoryInfo di = new DirectoryInfo(Environment.GetFolderPath(

	var files = from f in di.EnumerateFiles()
				where f.Extension.ToLower() == ".jpg" || 
					f.Extension == ".png"
				select f;

	int max = files.Count() + 1;

	Random rnd = new Random();
	int r = rnd.Next(0, max);

	List<FileInfo> pics = new List<FileInfo>(files);

	string randpic = pics[r].FullName;

	BitmapImage img = new BitmapImage();
	img.SetSource(new FileStream(randpic, FileMode.Open));

	MyImage.Source = img;


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

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

© 2017 Microsoft