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
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