CodeGym /Courses /C# SELF /File, File System, and Path Basics

File, File System, and Path Basics

C# SELF
Level 35 , Lesson 0
Available

1. Intro

File — it's just a block of data that's physically stored on some drive: hard disk, SSD, flash drive, or even a floppy disk (say hi to your grandpa!). For a user, a file is something you can open, read, change, and save.

In programming, a file is a sequence of bytes with some name and location, and for you as a programmer, it's an object that has a name, a path to it (like an address), size, creation date, and so on. The most important thing: a file is persistent storage for data. Everything that happens in memory after your program starts usually disappears when it ends, but a file — sticks around.

What's a file system?

File system — it's a way to store and organize files on a disk. It answers questions like: where and how to look for a file, who has access to it, how folders (directories) are organized, how to find a file's name and its contents.

Imagine a huge cabinet with lots of drawers. Each drawer can have folders, and each folder — documents (files). To find your C# notes, you have to open the cabinet, find the right drawer, pull out the folder, and inside it — the sheet you need. That's how a file system organizes info.

Popular file systems

  • NTFS — the "native" file system for Windows.
  • FAT32, exFAT — for flash drives, SD cards, external drives.
  • EXT4 — popular on Linux.
  • APFS, HFS+ — macOS.

For a C# dev, this usually doesn't matter: .NET hides the implementation details, and you work with the "file/folder" abstraction. But sometimes it's good to know a thing or two. For example, in NTFS you can use spaces and lots of special symbols in file names, but in FAT32 — nope.

2. How directories (folders) work

Directory (or folder) — it's a special type of file that holds lists of other files and/or directories. The directory tree (file hierarchy) is that familiar path from the root to a file: for example, C:\Users\Ivan\Documents\MyFile.txt.

  • Root — that's the very start of the drive (C:\).
  • Inside — folders (subfolders), which have files or other folders in them.

Here's a simple visualization:


C:\
 └── Users\
     └── Ivan\
         ├── Documents\
         │    └── MyFile.txt
         └── Pictures\
              └── Cat.jpg

3. What's a file path?

File path — it's the "address" of a file in the directory tree. With it, you can uniquely find the file or folder you want.

Absolute and relative path

  • Absolute path: starts from the root of the file system.
    Windows: C:\Users\Ivan\Documents\MyFile.txt
    Linux/macOS: /home/ivan/Documents/MyFile.txt
  • Relative path: starts from the current location (current directory).
    For example, if your program started from the folder C:\Users\Ivan, then the relative path Documents\MyFile.txt will get you to the same file.

Current directory — that's a special concept: it's the "starting point" for all relative paths. When you run a program, it's usually the folder where the .exe file is, or where you opened the console.

Diagram: Absolute and relative path


Absolute path                                 Relative path

C:\Projects\MyApp\logs\output.txt             logs\output.txt
└─ Drive root (C:)                            └─ Relative to where MyApp started

Path separators: \ and /

On Windows, you use backslash (\), and on Linux/macOS — usually forward slash (/).

Modern .NET supports both, but try not to mix them in one project. Always use the special API (application programming interface) to build paths, not just string concatenation.

4. How a program sees a file

.NET gives you a bunch of tools to work with files. The main ones:

  • System.IO.File: static methods for quick reading, writing, deleting, checking if files exist, etc.
  • System.IO.Directory: similar methods for working with folders.
  • System.IO.FileInfo and System.IO.DirectoryInfo: object wrappers with access to file/folder metadata.
  • System.IO.Path: helper class for working with paths (joining paths, getting extension, name, and so on).

A few examples

Checking if a file exists


using System.IO;

if (File.Exists("data.txt"))
{
    Console.WriteLine("File found!");
}
else
{
    Console.WriteLine("File not found!");
}

Getting file size


var info = new FileInfo("data.txt");
Console.WriteLine($"File size: {info.Length} bytes");

We'll break down how all these examples work in upcoming lectures. For now — just look and enjoy :)

5. Handy things to know

File metadata: name, extension, size, dates, etc.

When you work with a file, it's not just about the contents — there's also the "file passport":

  • Name — what the file is called for humans.
  • Extension.txt, .jpg, .exe, .cs and so on: helps (sometimes!) to guess what's inside.
  • Size — number of bytes.
  • Creation/modification time — you can find out when the file appeared or was last changed.
  • Attributes — for example: read-only, hidden, system file.

In .NET, classes like FileInfo, DirectoryInfo are super handy for this (we'll cover them in one of the next lectures).

File extensions and formats

File extension — it's just the part of the name after the dot. For example, document.txt has the extension txt. The OS uses the extension to figure out what to "open" the file with (like .jpg — an image viewer, .xls — Excel, etc).

But for a programmer, the extension is just a hint. The real file format is only defined by its contents. You could, for example, open MyCat.jpg as a text file — and see a bunch of "gibberish" (weird symbols, definitely not a picture). So always remember: extension — not a guarantee, just a convention.

How reading and writing files works: the basics

A file is a sequence of bytes. Working with a file in C# usually means:

  • Opening the file for reading or writing (or both).
  • Reading data into memory.
  • Writing new data to disk.
  • Closing the file — super important, otherwise you lock a system resource (and someone else can't delete the file while you're holding it open).

Modern APIs in .NET have your back: for example, the File class can open a file, read, and close it right away. But when working with streams (Stream) and big files, you gotta take this seriously (we'll talk about it in later lectures).

What to know about cross-platform stuff

Pretty much all modern C# works the same on Windows, Linux, and macOS. But there are still some gotchas. The most common — the difference in separators (\ vs /), and also limits on file name length and forbidden symbols.

That's why APIs like Path.Combine and methods from System.IO.Path are your best friends. They'll "glue" the path together the right way on any OS. Never write stuff like this:


var filePath = "C:\\Users\\Ivan\\data.txt"; // Windows only!

Better:


var filePath = Path.Combine("Users", "Ivan", "data.txt");

How to find out the current working directory?

Sometimes it's important to know where your files will actually be saved. For that, there's a special class:


string currentDir = Environment.CurrentDirectory;
Console.WriteLine($"Current directory: {currentDir}");

By the way, if you want to let the user pick a path, use file/folder picker dialogs or command line arguments.

2
Task
C# SELF, level 35, lesson 0
Locked
Getting File Information
Getting File Information
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION