Path methods in C#


To handle File Paths in C#, use the Path methods. These methods come under System.IO Namespace.

Some of them are −

GetExtension

Retrieve the extension of the file using the GetExtension() method.

For example, .txt, .dat, etc.

GetFileName

Retrieve the name of the file using the GetFileName() method.

For example, new.txt, details.dat, etc.

GetFileNameWithoutExtension

Retrieve the name of the file without extension using the GetFileNameWithoutExtension() method.

For example, new, details, etc.

Let us see an example −

Example

 Live Demo

using System.IO;
using System;
class Program {
   static void Main() {
      string myPath = "D:\one.txt";
      string fileExtension = Path.GetExtension(myPath);
      string fileName = Path.GetFileName(myPath);
      string noExtension = Path.GetFileNameWithoutExtension(myPath);

      Console.WriteLine("File Extension: "+fileExtension);
      Console.WriteLine("Fine Name: "+fileName);
      Console.WriteLine("File Name without extension: "+noExtension);
   }
}

Output

File Extension: .txt
Fine Name: D:\one.txt
File Name without extension: D:\one

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 22-Jun-2020

176 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements