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


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

Set the file name in a string −

string myPath = "D:\
ew\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:\
ew\quiz.txt";          Console.WriteLine(Path.GetExtension(myPath));       }    } }

Output

.txt

Updated on: 22-Jun-2020

193 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements