C# GetFileName: Retrieve File Names Easily

In C#, the method to obtain a file name usually involves using the Path class in the System.IO namespace. Specifically, the GetFileName method in the Path class can be used to retrieve the file name. An example is shown below:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = @"C:\Users\user\Desktop\example.txt";

        string fileName = Path.GetFileName(filePath);

        Console.WriteLine("File Name: " + fileName);
    }
}

In the example code above, a file path named filePath is defined first, then the file name is obtained using the Path.GetFileName method, and finally the file name is output to the console.

bannerAds