C# program to get the file name in C#

In C#, you can extract the file name from a full path using the Path.GetFileName() method from the System.IO namespace. This method returns only the file name and extension, without the directory path.

Syntax

Following is the syntax for using Path.GetFileName() method −

string fileName = Path.GetFileName(filePath);

Parameters

  • filePath − A string containing the full path to the file.

Return Value

The method returns a string containing the file name and extension. If the path ends with a directory separator, it returns an empty string.

Using Path.GetFileName() Method

Example

using System;
using System.IO;

class Program {
   static void Main(string[] args) {
      string myPath = "D:\new\quiz.txt";
      
      // get file name with extension
      string fileName = Path.GetFileName(myPath);
      Console.WriteLine("File Name: " + fileName);
      
      // get extension
      Console.WriteLine("Extension: " + Path.GetExtension(myPath));
      
      // get directory path
      Console.WriteLine("Directory: " + Path.GetDirectoryName(myPath));
   }
}

The output of the above code is −

File Name: quiz.txt
Extension: .txt
Directory: D:\new

Using Path.GetFileNameWithoutExtension() Method

To get only the file name without the extension, use the Path.GetFileNameWithoutExtension() method −

Example

using System;
using System.IO;

class Program {
   static void Main(string[] args) {
      string filePath = "C:\Documents\report.pdf";
      
      // get file name with extension
      string fullName = Path.GetFileName(filePath);
      Console.WriteLine("Full Name: " + fullName);
      
      // get file name without extension
      string nameOnly = Path.GetFileNameWithoutExtension(filePath);
      Console.WriteLine("Name Only: " + nameOnly);
      
      // get extension only
      string extension = Path.GetExtension(filePath);
      Console.WriteLine("Extension: " + extension);
   }
}

The output of the above code is −

Full Name: report.pdf
Name Only: report
Extension: .pdf

Handling Different Path Formats

Example

using System;
using System.IO;

class Program {
   static void Main(string[] args) {
      string[] paths = {
         "D:\folder\file.txt",
         "/home/user/document.pdf",
         "C:\temp",
         "readme.md",
         ""
      };
      
      foreach (string path in paths) {
         string fileName = Path.GetFileName(path);
         Console.WriteLine($"Path: '{path}' => File Name: '{fileName}'");
      }
   }
}

The output of the above code is −

Path: 'D:\folder\file.txt' => File Name: 'file.txt'
Path: '/home/user/document.pdf' => File Name: 'document.pdf'
Path: 'C:\temp' => File Name: ''
Path: 'readme.md' => File Name: 'readme.md'
Path: '' => File Name: ''

Comparison of Path Methods

Method Returns Example Input Example Output
Path.GetFileName() File name with extension "C:\docs\file.txt" "file.txt"
Path.GetFileNameWithoutExtension() File name without extension "C:\docs\file.txt" "file"
Path.GetExtension() File extension only "C:\docs\file.txt" ".txt"
Path.GetDirectoryName() Directory path "C:\docs\file.txt" "C:\docs"

Conclusion

The Path.GetFileName() method provides a simple way to extract file names from full paths in C#. Use Path.GetFileNameWithoutExtension() when you need only the name without the extension, and combine with other Path methods for complete file path manipulation.

Updated on: 2026-03-17T07:04:35+05:30

708 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements