C# program to get the extension of a file in C#

CsharpProgrammingServer Side Programming

To handle file paths, the Path class is used in C#.

Set the file name in a string −

string myPath = "D:\\new\\quiz.txt";

Now, to get the extension, use the GetExtension() method −

Path.GetExtension(myPath)

Here is the complete code −

Example

 Live Demo

using System;
using System.IO;
namespace Demo {
   class Program {
      static void Main(string[] args) {
         string myPath = "D:\\new\\quiz.txt";
         Console.WriteLine(Path.GetExtension(myPath));
      }
   }
}

Output

.txt
raja
Published on 05-Sep-2018 11:45:00
Advertisements