MSDN Documentation

FileInfo Class

Represents a file, provides instance members for creating, deleting, moving, and opening files, and properties for obtaining various information about the file.

Inheritance

System.Object
System.MarshalByRefObject
System.IO.FileSystemInfo
System.IO.FileInfo

Constructors

  • FileInfo(string fileName)

    public FileInfo(string fileName)

    Initializes a new instance of the FileInfo class.

    Parameters
    fileName
    A relative or absolute path that resolves to a file.

Properties

  • Exists

    public bool Exists { get; }

    Gets a value indicating whether the file referenced by this FileInfo instance exists.

  • Name

    public string Name { get; }

    Gets the name of the file or directory.

  • FullName

    public string FullName { get; }

    Gets the fully qualified path of the file or directory.

  • Directory

    public DirectoryInfo Directory { get; }

    Gets an object representing the directory containing this file.

  • Length

    public long Length { get; }

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

  • CreationTime

    public DateTime CreationTime { get; }

    Gets or sets the date and time, in the current Coordinated Universal Time (UTC) information, that the current file was created.

  • LastAccessTime

    public DateTime LastAccessTime { get; }

    Gets or sets the date and time that the current file was last accessed.

  • LastWriteTime

    public DateTime LastWriteTime { get; }

    Gets or sets the date and time that the current file was last written to.

Methods

  • Create()

    public FileStream Create()

    Creates or overwrites a file in the specified path.

    Returns: A new FileStream object writing to the specified path.

  • Delete()

    public void Delete()

    Deletes this file.

  • MoveTo(string destFileName)

    public void MoveTo(string destFileName)

    Moves a specified file to a new location and optionally allows the existing file to be overwritten.

    Parameters
    destFileName
    The new path and name of the file. This must be a file name, not a directory name.
  • Open(FileMode mode)

    public FileStream Open(FileMode mode)

    Opens a file in the specified mode.

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

    Returns: A FileStream object on the specified path that matches the specified mode.

  • OpenRead()

    public FileStream OpenRead()

    Opens an existing file for reading.

    Returns: A FileStream object that can be used to read from the file.

  • OpenWrite()

    public FileStream OpenWrite()

    Opens an existing file for writing.

    Returns: A FileStream object that can be used to write to the file.

Example Usage

The following example demonstrates how to create a FileInfo object and get information about the file.


using System;
using System.IO;

public class Example
{
    public static void Main()
    {
        string filePath = "myFile.txt";

        // Create a dummy file for the example
        File.WriteAllText(filePath, "This is a test file.");

        FileInfo fileInfo = new FileInfo(filePath);

        if (fileInfo.Exists)
        {
            Console.WriteLine($"File Name: {fileInfo.Name}");
            Console.WriteLine($"Full Path: {fileInfo.FullName}");
            Console.WriteLine($"File Size: {fileInfo.Length} bytes");
            Console.WriteLine($"Creation Time: {fileInfo.CreationTime}");
            Console.WriteLine($"Last Access Time: {fileInfo.LastAccessTime}");
            Console.WriteLine($"Last Write Time: {fileInfo.LastWriteTime}");

            // Example of opening and reading
            using (FileStream fs = fileInfo.OpenRead())
            using (StreamReader reader = new StreamReader(fs))
            {
                Console.WriteLine("\nFile Content:");
                Console.WriteLine(reader.ReadToEnd());
            }

            // Example of deleting the file
            // fileInfo.Delete();
            // Console.WriteLine("\nFile deleted.");
        }
        else
        {
            Console.WriteLine($"File '{filePath}' does not exist.");
        }
    }
}