File, FileInfo and Path

File and Directory check for, create, list, copy and delete files and folders. FileInfo describes one file. Path builds paths with Path.Combine and takes them apart, without touching the disk.

Apprentice · C# · Updated

After this lesson you can

  • Check for, create, list and delete files and folders with File and Directory
  • Build a path with Path.Combine, and take one apart with Path's methods

Key takeaways

  • Path.Combine("data", "2026", "orders.csv") joins the parts with the right separator, so you never type a backslash.
  • Path methods such as GetFileName and GetExtension work only on the string and never look at the disk.
  • File and Directory touch the disk: File.Exists, Directory.CreateDirectory, Directory.GetFiles, File.Copy, File.Move and File.Delete.
  • File, Directory and Path are static classes you call with no new; FileInfo is an object you create with new to read one file's details.

System.IO has a small set of classes for working with the file system. Three you'll use all the time:

Class What it's for
File Files: does one exist? Copy, move, delete, read, write
Directory Folders: does one exist? Create, list what's inside, delete
Path Path strings: join them, take them apart

File, Directory and Path are static classes: you call their methods on the class itself, as you do with Math.Max, with no new.

Path: building and taking apart

Path.Combine joins folder and file names into one path, with the right separator between each part:

string path = Path.Combine("data", "2026", "orders.csv");
Console.WriteLine(path);
data\2026\orders.csv

No backslashes to type, none to double, and none missing or doubled when one part already ends with one.

Other Path methods take a path apart:

string path = @"C:\bootcamp\data\orders.csv";

Console.WriteLine(Path.GetFileName(path));
Console.WriteLine(Path.GetFileNameWithoutExtension(path));
Console.WriteLine(Path.GetExtension(path));
Console.WriteLine(Path.GetDirectoryName(path));
Console.WriteLine(Path.ChangeExtension(path, ".txt"));
orders.csv
orders
.csv
C:\bootcamp\data
C:\bootcamp\data\orders.txt

Path only works on the string. None of these methods looks at the disk, so they work whether the file exists or not.

File and Directory: asking and changing

These do touch the disk. Here's a program that makes a folder, puts a file in it, and looks around:

string folder = "reports";
Directory.CreateDirectory(folder);

string path = Path.Combine(folder, "monday.txt");
Console.WriteLine(quot;Before: {File.Exists(path)}");

File.WriteAllText(path, "Sold 12 tickets");
Console.WriteLine(quot;After: {File.Exists(path)}");

File.Copy(path, Path.Combine(folder, "tuesday.txt"), overwrite: true);

foreach (string file in Directory.GetFiles(folder))
{
    Console.WriteLine(Path.GetFileName(file));
}

File.Delete(path);
Console.WriteLine(quot;Deleted: {!File.Exists(path)}");
Before: False
After: True
monday.txt
tuesday.txt
Deleted: True

(File.WriteAllText creates a file holding that text. Writing to Files in C# covers it properly.)

The ones to know:

Method What it does
File.Exists(path) true if there's a file at that path
Directory.Exists(path) true if there's a folder at that path
Directory.CreateDirectory(path) Creates the folder, and any missing folders above it. Does nothing if it's already there.
Directory.GetFiles(path) The paths of the files in a folder. Directory.GetFiles(path, "*.csv") gives only the CSV files.
File.Copy(from, to) Copies a file. Add overwrite: true to replace a file that's already there.
File.Move(from, to) Moves or renames a file
File.Delete(path) Deletes a file. Does nothing if it isn't there.

FileInfo: one file's details

When you want to know more about a file, make a FileInfo for its path. Unlike File, it's an object you create with new, and its properties describe the file:

File.WriteAllText("notes.txt", "Bring snacks");

FileInfo info = new FileInfo("notes.txt");
Console.WriteLine(info.Name);
Console.WriteLine(info.Exists);
Console.WriteLine(quot;{info.Length} bytes");
Console.WriteLine(info.Extension);
notes.txt
True
12 bytes
.txt

info.LastWriteTime tells you when the file last changed, and info.FullName gives its absolute path. Like a path, creating a FileInfo doesn't create a file: for a missing file, Exists is false.

Check yourself

2 questions to try on the lesson page.

So what

Path builds and splits path strings, File and Directory ask about and change the disk, and FileInfo describes one file. With those, your program can find its way around any folder.

Key terms

Cheat sheets