FileInfo Class
- Similar to the drives and folders, the System.IO namespace is used to manipulate the files also.
- The file manipulations include with checking for the file existence, get the file details like file size, file attributes, created date, last accessed date etc., and write new content to the files and also read content from existing files.
- Every file is to be represented as an object, created for System.IO.FileInfo class.
Library: System.IO.FileInfo
This class object represents a file on the file system. This able to get the information of the file and also to perform certain operations on that folder.
Object Construction:
Syn: FileInfo obj = new FileInfo(path of the file);
Ex: FileInfo obj = new FileInfo(c:\\tc\list.h);
You can observe the list of all available properties, methods of this class.
Program for FileInfo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace FileInfoDemo
{
class Program
{
static void Main(string[] args)
{
string filepath;
Console.WriteLine("Enter the file path:");
filepath = Console.ReadLine();
FileInfo fobj = new FileInfo(filepath);
if (fobj.Exists)
{
Console.WriteLine("\nName: " + fobj.Name);
Console.WriteLine("Full Name: " + fobj.FullName);
Console.WriteLine("Extension: " + fobj.Extension);
Console.WriteLine("Directory: " + fobj.DirectoryName);
Console.WriteLine("File Size: " + fobj.Length + " bytes");
Console.WriteLine("Created on: " + fobj.CreationTime);
Console.WriteLine("Last Accessed on: " + fobj.LastAccessTime);
Console.WriteLine("Last Modified on: " + fobj.LastWriteTime);
}
else
Console.WriteLine("File not found.");
Console.Read();
}
}
}
No comments:
Post a Comment