C# Paths
Name a place with the Path helpers in System.IO so you can ask questions about text before any file is opened. Keep the work in Pine with Main. Call Path.GetFileName("note.txt") and print the result. Run with dotnet run. The screen should show note.txt. A second demo joins "docs" and "note.txt" with Path.Combine, prints the file name, then prints the directory piece. A third demo prints the whole joined path. Leave inheritance for the next page. Keep the starting public class named Pine.
Name one file piece
The writing page stored letters with File.WriteAllText and a plain string "note.txt". This page keeps that same kind of string and asks path questions about it. Open the Pine file. The starting class name stays Pine. This page does not rename the starter. Delete leftover File.WriteAllText demos that store tea or oak. Delete leftover File.ReadAllLines demos. Delete leftover throw and catch blocks. Delete any ReadLine line. The program must not wait for typing.
At the top keep both:
using System;
using System.IO;
Inside Main, ask for the last piece of one name:
string name = Path.GetFileName("note.txt");
Then print it:
Console.WriteLine(name);
Naming is not the same as opening. You do not need a note.txt file on disk for these lines to run, because Path.GetFileName only reads the text you give it. The method works on the string, not on the disk.
using System;
using System.IO;
public class Pine
{
public static void Main()
{
string name = Path.GetFileName("note.txt");
Console.WriteLine(name);
}
}
Save, then run with dotnet run. The screen shows note.txt. That is the last piece of the name you gave. Path.GetFileName does not open the file, does not check whether it exists, and does not return a handle. It reads the text and returns the final part. Keep using System.IO; or Path will not resolve. Spelling of the method name matters: GetFileName, with capital letters where the language expects them.
If the terminal waits for keyboard input, a ReadLine call from an older exercise is still in the file. Delete that call. If the screen shows tea or oak, a write or read leftover is still active — replace it with the path demo. If you see blocked or after, an old try-catch file is still the one you are running. If you see empty or a stack from ArgumentException, a throwing leftover is still in the project.
Walk the one-name naming run
C# starts at Pine.Main. Path.GetFileName receives the string "note.txt". It finds the last piece after any separator, and in this name there is no folder, so the whole text is the last piece. The variable name holds that result. Console.WriteLine prints it. Nothing else is written, and no file is opened on disk.
That quiet naming is the point of this first demo. A path can exist as text even when no file was prepared beside the project. The lesson is to keep the question separate from the answer: you ask about the name first, and only later decide whether a read or a write should touch the disk.
Notice that Path here is a static helper class from System.IO. You call methods on the class name directly. You do not create a Path object with new, and you do not call .getFileName() on a value. The helper works on a string you already have.
Join a folder name and a file name
A second program builds a path from two pieces: folder docs, then file note.txt. Path.Combine joins them in order. Then Path.GetFileName gives the last piece, and Path.GetDirectoryName gives the folder piece above it.
using System;
using System.IO;
public class Pine
{
public static void Main()
{
string joined = Path.Combine("docs", "note.txt");
Console.WriteLine(Path.GetFileName(joined));
Console.WriteLine(Path.GetDirectoryName(joined));
}
}
Save, then run with dotnet run. The first line is note.txt. The second line is docs. GetFileName gives only the last piece. GetDirectoryName gives the folder piece that sits before it. Do not swap those meanings: the directory is not note.txt, and the file name is not docs.
Path.Combine puts the separator between the pieces for you. On Windows the joined text often shows a backslash, and on Linux and macOS it often shows a forward slash. The teaching point is that the pieces are joined in order, not that you memorize one separator character. Combine builds the text only. It does not create a folder or a file on disk.
If you pass only "note.txt" to GetDirectoryName, the result is an empty string, because a lone name has no folder piece in front of it. The two-piece demo keeps a clear directory so the screen shows docs. Keep using System.IO; on every paths teaching file.
Print the whole joined path
A third program keeps Path.Combine("docs", "note.txt") and prints the joined text itself. This shows the value the other helpers were reading.
using System;
using System.IO;
public class Pine
{
public static void Main()
{
string joined = Path.Combine("docs", "note.txt");
Console.WriteLine(joined);
}
}
Run again with dotnet run. The screen shows the whole path as one line, such as docs\note.txt on Windows or docs/note.txt on Linux. The exact separator follows the operating system that runs the program. Prefer reading GetFileName and GetDirectoryName when you want one piece alone. If the two-line file from the previous demo still feels clearer, keep it nearby while you study the single joined print.
Do not invent a Java-style Path object on this page. Do not call getFileName() or getParent(); those are the Java names, and C# uses Path.GetFileName and Path.GetDirectoryName as static calls on strings. Do not paste File.WriteAllText into the paths file as a requirement — writing already had its own lesson.
Compared with writing and reading
On the writing page, dotnet run stored tea and then oak in note.txt with File.WriteAllText. On the files page, dotnet run printed a line from disk with File.ReadAllLines. On this page, dotnet run only names places with Path helpers. Keep the write and read files nearby only as history when you want to check what landed on disk. Run the paths file for these name demos so the screen matches this lesson.
The new tokens are Path.Combine, Path.GetFileName, and Path.GetDirectoryName. Each one works on text. None of them touches the disk. File is for disk work. Path is for text that describes where something could live.
Separators and the stop you do not need
Beginners often expect a path to fail as soon as the file is missing. That is a read or write story, not a naming story. Path.GetFileName("missing.txt") still prints missing.txt, because no disk check happens. The FileNotFoundException from the files page only appears when you call File.ReadAllLines on a name that does not exist. Keep the two ideas apart in your notes: naming is safe on any text; opening is where the disk can refuse.
Do not add try or catch to the paths demos to hide a missing file. This page never opens one. If you want to see a read stop again, go back to the files lesson and run that file deliberately.
What this leaves for inheritance
This page names paths with helpers that work on text. The next page begins inheritance, where one class can build on another. Stay with Path.Combine, Path.GetFileName, and Path.GetDirectoryName here. Do not add a class hierarchy as a requirement on these Pine path demos yet.
Before inheritance
You can call Path.GetFileName("note.txt") and see note.txt. You can join "docs" and "note.txt" with Path.Combine, see note.txt from GetFileName, then see docs from GetDirectoryName. You can print the whole joined path as one line. Run Pine with dotnet run for each kept demo in turn. Keep the separator as whatever your operating system prints; the pieces stay the same.
Think of a path here as a label you can slice. The label can be written down before anything exists on disk. Writing and reading are later choices that use the label. Inheritance next will be another kind of structure, built from classes rather than strings.
Checkpoint on the path names
Keep the single-name demo for a clear GetFileName result. Keep the two-piece demo for a clear directory result. Keep the joined print when you want one screen line. Keep inheritance off this page. Keep using System.IO; on every paths file. Those habits are the checkpoint before you move on. When note.txt and docs appear on the two-line demo of dotnet run, paths did their job for this lesson and you are ready for the next topic about inheritance.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top