C# program to get the file name in C#

CsharpProgrammingServer Side Programming

Set the path name in a string −

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

Now, use the GetFileName() method to get the name of the file −

Path.GetFileName(myPath)

The following 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";
         // get extension
         Console.WriteLine("Extension: "+Path.GetExtension(myPath));
         // get path
         Console.WriteLine("File Path: "+Path.GetFileName(myPath));
      }
   }
}

Output

Extension: .txt
File Path: D:\new\quiz.txt
raja
Published on 05-Sep-2018 11:48:08
Advertisements