Path methods in C#

ProgrammingServer Side ProgrammingCsharp

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
raja
Published on 11-Sep-2018 08:30:43
Advertisements