What Is Fileinfo Class In C#?

 Admin   
196 View    Jan 17 2020 10:59PM

File Info:

Class in c# is utilized for controlling record as making, erasing, evacuating, replicating, opening and getting data. It gives properties and case techniques that make record control simple.

FileInfo class is used to copy, transfer, rename, build, open, delete and add the file usually.

Example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.IO;

 

namespace FileInfo_Class

{

    class Program

    {

        static void Main(string[] args)

        {

            string path = @"D:\csharpmonster\fileinfo.txt";

            FileInfo file = new FileInfo(path);

            //Create File

            using (StreamWriter sw = file.CreateText())

            {

                sw.WriteLine("Hello FileInfo");

            }

 

            //Display File Info           

            Console.WriteLine("File Create on : " + file.CreationTime);

            Console.WriteLine("Directory Name : " + file.DirectoryName);

            Console.WriteLine("Full Name of File : " + file.FullName);

            Console.WriteLine("File is Last Accessed on : " + file.LastAccessTime);

           

            //Deleting File

            Console.WriteLine("Press small y for delete this file");

            try

            {

                char ch = Convert.ToChar(Console.ReadLine());

                if (ch == 'y')

                {

                    if (file.Exists)

                    {

                        file.Delete();

                        Console.WriteLine(path + " Deleted Successfully");

                    }

                    else

                    {

                        Console.WriteLine("File doesn't exist");

                    }

                }               

            }

            catch

            {

                Console.WriteLine("Press Enter to Exit");

            }

            Console.ReadKey();

        }

    }

}

 

Output:

File Create on : 7-01-2020 PM 12:49:36

 

Directory Name : D:\csharpmonster

 

Full Name of File : D:\csharpmonster\fileinfo.txt

 

File is Last Accessed on: 7-01-2020 PM 12:49:36