How to Extract filename from a given path in C# Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report While developing an application that can be desktop or web in C#, such kind of requirement to extract the filename from a given path (where the path can be taken while selecting a file using File Open dialog box or any other sources) can arise. A path may contain the drive name, directory name(s) and the filename. To extract filename from the file, we use "GetFileName()" method of "Path" class. This method is used to get the file name and extension of the specified path string. The returned value is null if the file path is null. Syntax: public static string GetFileName (string path); Here, path is the string from which we have to obtain the file name and extension. Return Value: This method will return the characters after the last directory separator character in path. If the last character of the path is a directory or volume separator character, this method returns Empty. If the path is null, this method returns null. Exception: This method will give ArgumentException if the path contains one or more of the invalid characters defined in GetInvalidPathChars(). Examples: Input : string strPath = "c://myfiles//ref//file1.txt"; //function call to get the filename filename = Path.GetFileName(strPath); Output : file1.txt CSharp // C# program to extract the // filename from a given path using System; using System.IO; using System.Text; namespace Geeks { class GFG { // Main Method static void Main(string[] args) { // taking full path of a file string strPath = "C:// myfiles//ref//file1.txt"; // initialize the value of filename string filename = null; // using the method filename = Path.GetFileName(strPath); Console.WriteLine("Filename = " + filename); Console.ReadLine(); } } } Output: Filename = file1.txt Reference: https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getfilename?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# Program to Check the Information of the File S SudhirSharma2 Follow Improve Article Tags : C# Similar Reads C# Program For Listing the Files in a Directory Given files, now our task is to list all these files in the directory using C#. So to do this task we use the following function and class: DirectoryInfo: It is a class that provides different types of methods for moving, creating, and enumerating through directories and their subdirectories. You ca 2 min read Check if a path has a file name extension in C# Path.HasExtension Method is used to check whether the specified path has a file name extension or not. This method will start the searching for a period (.) followed by at least one character from the end of the path. If this pattern is found before a DirectorySeparatorChar, AltDirectorySeparatorCha 2 min read C# Program to Check the Information of the File Given a file, now our task is to view the information of this file through C#. So to do this task we use FileInfo class. This class provides different types of properties and methods for creating, copying, deleting, opening, and moving files. It is also, used for creating FileStream objects. So here 1 min read File.GetCreationTime() Method in C# with Examples File.GetCreationTime(String) is an inbuilt File class method which is used to return the creation date and time of the specified file or directory. Syntax: public static DateTime GetCreationTime (string path); Parameter: This function accepts a parameter which is illustrated below: path: This is the 2 min read C# Program to Search Directories and List Files Given files and directories, now our task is to search these files and directories using C#. So to do this task we use the following methods: 1. SearchOption: This method is used to specify whether to search the current directory or the current directory with all subdirectories. Syntax: public enum 2 min read Basics Operations of File and Directory in C# In this article, we are going to cover how to create, delete and rename directory and also how to delete and rename the file.  Creating a Directory We can create Directory using CreateDirectory() method present in the Directory class. csharp // C# program to create a directory using System; using 4 min read Like