How to Get File Name, Path, Directory, Extension in C#

Hi programmers, welcome to new post of c# .net. this article i’ll write codes to print File Name, path , extension ,directory name , create time , update time last access time in c# console application. we should use FileInfo class and create object of FileInfo and call properties. Let’s see the program.

Use Directory this below codes into Editor.

using System;
using System.IO;
namespace ConsoleApp7
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = @"C:\Users\Adam\Desktop\James\AddNode.xml";
            FileInfo fi = new FileInfo(str);

            string f1 = fi.Name, f2 = fi.FullName, f3 = fi.Extension, f4 = fi.DirectoryName;
                   ;
            Console.WriteLine("File Name: {0},\nPath : {1},\nExtension : {2},\n" +
                "Directory : {3}\n", f1,f2,f3,f4);

            DateTime d1 = fi.CreationTime, d2 = fi.LastWriteTime,d3=fi.LastAccessTime;
            Console.WriteLine("Creation time: {0},\nLast access time: {1},\nLast write time: {2} \n", 
                                d1,d2,d3);            
        }
    }
}

Happy Coding…Thanks.

Post Author: adama