FileInfo Class

System.IO

Provides an instance of a file, so that you can create, delete, move, and open a file. This class is immutable. If the state of the file represented by this object changes, this object will not reflect that change.

Declaration

public sealed class FileInfo : FileSystemInfo

Overview

The FileInfo class provides instance members that you can use to create, delete, move, and open files. It also provides members for reading metadata about a file, such as its size, creation time, and last accessed time.

Remarks

To get a FileInfo object, you can use the constructor or the DirectoryInfo.GetFiles method.

When you use the constructor to create a FileInfo object, the file does not need to exist. However, many of the methods and properties of FileInfo will throw an exception if the file does not exist.

Members

Constructors

Constructor Description
Initializes a new instance of the FileInfo class for the specified file path.

Parameters

  • fileName
    System.String
    A relative or absolute path that is valid (including Universal Naming Convention (UNC) paths).

Properties

Property Type Description
System.IO.FileAttributes Gets or sets the specified attributes of the file.
This property is inherited from FileSystemInfo.
System.DateTime Gets the creation date and time of the file.
This property is inherited from FileSystemInfo.
System.DateTime Gets the creation date and time, in Coordinated Universal Time (UTC), of the file.
This property is inherited from FileSystemInfo.
System.IO.DirectoryInfo Gets a DirectoryInfo object that represents the directory containing the file.
System.String Gets the name of the directory that contains the file.
This property is inherited from FileSystemInfo.
System.String Gets the extension of the file.
This property is inherited from FileSystemInfo.
System.Boolean Gets a value indicating whether the file exists.
This property is inherited from FileSystemInfo.
System.String Gets the full path of the file.
This property is inherited from FileSystemInfo.
System.Boolean Gets a value indicating whether the current file is read-only.
System.DateTime Gets the date and time the current file was last accessed.
This property is inherited from FileSystemInfo.
System.DateTime Gets the date and time, in Coordinated Universal Time (UTC), the current file was last accessed.
This property is inherited from FileSystemInfo.
System.DateTime Gets the date and time when the current file was last written to.
This property is inherited from FileSystemInfo.
System.DateTime Gets the date and time, in Coordinated Universal Time (UTC), when the current file was last written to.
This property is inherited from FileSystemInfo.
System.String Gets the name of the file.
This property is inherited from FileSystemInfo.
System.Int64 Gets the size, in bytes, of the current file.

Methods

Method Description
Creates or overwrites a file in the specified path.

Returns

System.IO.FileStream
A new FileStream object.

Exceptions

  • System.IO.IOException: The directory specified does not exist.
  • System.UnauthorizedAccessException: The caller does not have the required permission.
Creates a new file or overwrites an existing file with the specified path.

Returns

System.IO.StreamWriter
A new StreamWriter object.
Deletes this file.

Exceptions

  • System.IO.IOException: The file is in use and cannot be accessed by the calling process.
  • System.Security.SecurityException: The caller does not have the required permission.
  • System.IO.FileNotFoundException: The file does not exist.
Moves and renames a file.

Parameters

  • destFileName
    System.String
    The new path for the file.

Exceptions

  • System.ArgumentNullException: destFileName is null.
  • System.ArgumentException: destFileName is an empty string, contains only white space, or contains one or more invalid characters.
  • System.IO.IOException: The destination file exists, or the system cannot move the file.
  • System.UnauthorizedAccessException: The caller does not have the required permission.
Opens a file in the specified mode.

Parameters

  • mode
    System.IO.FileMode
    A FileMode value that specifies whether to open or create the file.

Returns

System.IO.FileStream
A FileStream object opened in the specified mode.
Opens a file in the specified mode and with the specified access.

Parameters

  • mode
    System.IO.FileMode
    A FileMode value that specifies whether to open or create the file.
  • access
    System.IO.FileAccess
    A FileAccess value that specifies whether to open the file for reading, writing, or both.

Returns

System.IO.FileStream
A FileStream object opened in the specified mode and with the specified access.
Opens an existing file for reading.

Returns

System.IO.FileStream
A FileStream that can be used to read the file.
Opens an existing UTF-8 encoded text file for reading.

Returns

System.IO.StreamReader
A StreamReader object.
Opens an existing file or creates a new file for writing.

Returns

System.IO.FileStream
A FileStream opened for writing.
Refreshes the state of the object.
This method is inherited from FileSystemInfo.

Example

The following example demonstrates how to use the FileInfo class to create, write to, read from, and delete a file.

using System;
using System.IO;

public class Example
{
public static void Main()
{
string fileName = "MyFile.txt";
FileInfo fileInfo = new FileInfo(fileName);

// Create the file and write to it
using (StreamWriter sw = fileInfo.CreateText())
{
sw.WriteLine("This is the first line.");
sw.WriteLine("This is the second line.");
}
Console.WriteLine($"File '{fileName}' created and written to.");

// Check if the file exists and get its size
if (fileInfo.Exists)
{
Console.WriteLine($"File exists. Size: {fileInfo.Length} bytes.");

// Read from the file
Console.WriteLine("Reading from the file:");
using (StreamReader sr = fileInfo.OpenText())
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}

// Delete the file
fileInfo.Delete();
Console.WriteLine($"File '{fileName}' deleted.");
}
else
{
Console.WriteLine($"File '{fileName}' not found.");
}
}
}