FileInfo Class

Definition

Provides properties and 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.

[Serializable]
[ComVisible(true)]
public sealed class FileInfo : FileSystemInfo, ISerializable
Inheritance

Inherited Members

System.IO.FileSystemInfo

System.Object

Constructors

FileInfo(String)

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

public FileInfo(string fileName)
Parameters
fileName
String

The fully qualified name of the new file, or the relative file name. Do not end the path with the directory separator character.

Exceptions

fileName is null.

The caller does not have the required permission.

The file name is empty, contains only white spaces, or contains invalid characters.

Access to fileName is denied.

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

fileName contains a colon (:) in the middle of the string.

Properties

Directory

Gets an instance of the parent directory.

public DirectoryInfo Directory { get; }
Value
DirectoryInfo

A DirectoryInfo object representing the parent directory of this file.

Exceptions

The specified path is invalid, such as being on an unmapped drive.

The caller does not have the required permission.

DirectoryName

Gets a string representing the directory's full path.

public string DirectoryName { get; }
Value
String

A string representing the directory's full path.

Exceptions

null was passed in for the directory name.

The fully qualified path is 260 or more characters.

The caller does not have the required permission.

Exists

Gets a value indicating whether a file exists.

public override bool Exists { get; }
Value
Boolean

true if the file exists; false if the file does not exist or if the file is a directory.

Overrides

IsReadOnly

Gets or sets a value that determines if the current file is read only.

public bool IsReadOnly { get; set; }
Value
Boolean

true if the current file is read only; otherwise, false.

Exceptions

The file described by the current FileInfo object could not be found.

An I/O error occurred while opening the file.

This operation is not supported on the current platform.

-or-

The caller does not have the required permission.

The user does not have write permission, but attempted to set this property to false.

Length

Gets the size, in bytes, of the current file.

public long Length { get; }
Value
Int64

The size of the current file in bytes.

Exceptions

Refresh() cannot update the state of the file or directory.

The file does not exist.

-or-

The Length property is called for a directory.

Name

Gets the name of the file.

public override string Name { get; }
Value
String

The name of the file.

Overrides

Methods

AppendText()

Creates a StreamWriter that appends text to the file represented by this instance of the FileInfo.

public StreamWriter AppendText()
Returns

A new StreamWriter.

See Also

CopyTo(String)

Copies an existing file to a new file, disallowing the overwriting of an existing file.

public FileInfo CopyTo(string destFileName)
Parameters
destFileName
String

The name of the new file to copy to.

Returns

A new file with a fully qualified path.

Exceptions

destFileName is empty, contains only white spaces, or contains invalid characters.

An error occurs, or the destination file already exists.

The caller does not have the required permission.

destFileName is null.

A directory path is passed in, or the file is being moved to a different drive.

The directory specified in destFileName does not exist.

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

destFileName contains a colon (:) within the string but does not specify the volume.

CopyTo(String, Boolean)

Copies an existing file to a new file, allowing the overwriting of an existing file.

public FileInfo CopyTo(string destFileName, bool overwrite)
Parameters
destFileName
String

The name of the new file to copy to.

overwrite
Boolean

true to allow an existing file to be overwritten; otherwise, false.

Returns

A new file, or an overwrite of an existing file if overwrite is true. If the file exists and overwrite is false, an IOException is thrown.

Exceptions

destFileName is empty, contains only white spaces, or contains invalid characters.

An error occurs, or the destination file already exists and overwrite is false.

The caller does not have the required permission.

destFileName is null.

The directory specified in destFileName does not exist.

A directory path is passed in, or the file is being moved to a different drive.

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

destFileName contains a colon (:) in the middle of the string.

Create()

Creates a file.

public FileStream Create()
Returns

A new file.

CreateText()

Creates a StreamWriter that writes a new text file.

public StreamWriter CreateText()
Returns

A new StreamWriter.

Exceptions

The file name is a directory.

The disk is read-only.

The caller does not have the required permission.

See Also

Delete()

Permanently deletes a file.

public override void Delete()
Exceptions

The target file is open or memory-mapped on a computer running Microsoft Windows NT.

-or-

There is an open handle on the file, and the operating system is Windows XP or earlier. This open handle can result from enumerating directories and files. For more information, see How to: Enumerate Directories and Files.

The caller does not have the required permission.

The path is a directory.

Overrides

MoveTo(String)

Moves a specified file to a new location, providing the option to specify a new file name.

public void MoveTo(string destFileName)
Parameters
destFileName
String

The path to move the file to, which can specify a different file name.

Exceptions

An I/O error occurs, such as the destination file already exists or the destination device is not ready.

destFileName is null.

destFileName is empty, contains only white spaces, or contains invalid characters.

The caller does not have the required permission.

destFileName is read-only or is a directory.

The file is not found.

The specified path is invalid, such as being on an unmapped drive.

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

destFileName contains a colon (:) in the middle of the string.

Open(FileMode)

Opens a file in the specified mode.

public FileStream Open(FileMode mode)
Parameters
mode
FileMode

A FileMode constant specifying the mode (for example, Open or Append) in which to open the file.

Returns

A file opened in the specified mode, with read/write access and unshared.

Exceptions

The file is not found.

The file is read-only or is a directory.

The specified path is invalid, such as being on an unmapped drive.

The file is already open.

Open(FileMode, FileAccess)

Opens a file in the specified mode with read, write, or read/write access.

public FileStream Open(FileMode mode, FileAccess access)
Parameters
mode
FileMode

A FileMode constant specifying the mode (for example, Open or Append) in which to open the file.

access
FileAccess

A FileAccess constant specifying whether to open the file with Read, Write, or ReadWrite file access.

Returns

A FileStream object opened in the specified mode and access, and unshared.

Exceptions

The caller does not have the required permission.

The file is not found.

path is read-only or is a directory.

The specified path is invalid, such as being on an unmapped drive.

The file is already open.

Open(FileMode, FileAccess, FileShare)

Opens a file in the specified mode with read, write, or read/write access and the specified sharing option.

public FileStream Open(FileMode mode, FileAccess access, FileShare share)
Parameters
mode
FileMode

A FileMode constant specifying the mode (for example, Open or Append) in which to open the file.

access
FileAccess

A FileAccess constant specifying whether to open the file with Read, Write, or ReadWrite file access.

share
FileShare

A FileShare constant specifying the type of access other FileStream objects have to this file.

Returns

A FileStream object opened with the specified mode, access, and sharing options.

Exceptions

The caller does not have the required permission.

The file is not found.

path is read-only or is a directory.

The specified path is invalid, such as being on an unmapped drive.

The file is already open.

OpenRead()

Creates a read-only FileStream.

public FileStream OpenRead()
Returns

A new read-only FileStream object.

Exceptions

path is read-only or is a directory.

The specified path is invalid, such as being on an unmapped drive.

The file is already open.

OpenText()

Creates a StreamReader with UTF8 encoding that reads from an existing text file.

public StreamReader OpenText()
Returns

A new StreamReader with UTF8 encoding.

Exceptions

The caller does not have the required permission.

The file is not found.

path is read-only or is a directory.

The specified path is invalid, such as being on an unmapped drive.

See Also

OpenWrite()

Creates a write-only FileStream.

public FileStream OpenWrite()
Returns

A write-only unshared FileStream object for a new or existing file.

Exceptions

The path specified when creating an instance of the FileInfo object is read-only or is a directory.

The path specified when creating an instance of the FileInfo object is invalid, such as being on an unmapped drive.

ToString()

Returns the path as a string.

public override string ToString()
Returns

A string representing the path.

Overrides