File Manipulations

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

API: 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 directory”);
Ex: FileInfo obj = new FileInfo(“c:\\tc\list.h”);

You can observe the list of all available properties, methods of this class.
Properties of “FileInfo” class
Exists Checks the existence of the directory. If it exists, it indicates “true”. Otherwise, it indicates “false”.
Name Gets only the name of the file (without path)
FullName Gets the name along with full path.
Extension Gets the extension of the file.
DirectoryName Gets the name of the folder, in which the file exists.
Length Gets the size of the file (in bytes).
CreationTime Gets the date & time, when the file is created.
LastAccessTime Gets the date & time, when the file is accessed last time.
LastWriteTime Gets the date & time, when the file is modified last time.

Methods of “FileInfo” class
Delete() Deletes the file permanently.
CopyTo(destination file name) Copies the file into the destination location with the specified file name.
MoveTo(destination file name) Moves the file into the destination location with the specified file name.

Demo on “FileInfo” class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FileInfoDemo1
{
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("Name: " + fobj.Name);
Console.WriteLine("Full Name: " + fobj.FullName);
Console.WriteLine("Extension: " + fobj.Extension);
Console.WriteLine("Directory: " + fobj.DirectoryName);
Console.WriteLine("File Size: " + fobj.Length);
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();
}
}
}
Output:
Streams

File Streams

• To write / read the content to / write the file, you require file streams.
• A file stream acts as a pointer for the file, which contains the memory address of the file on the disc.
• There are two types of file streams.
1) Reading Streams
2) Writing Streams

API: System.IO.StreamReader (to read the content of the file)
System.IO.StreamWriter (to write the content into the file)
Reading content from the file
 Import the API
using System.IO;
 Create the stream reader object
StreamReader sr = new StreamReader(“file name”);
 Reade the content
sr.ReadToEnd();
 Close the Reader
sr.Close();
Writing content to the file
 Import the API
using System.IO;
 Create the stream writer object
StreamWriter sw = new StreamWriter(“file name”);
 Write the content
sw.Write(“content here”);
 Close the Writer
sw.Close();

File Reading

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace FileReadDemo
{
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)
{
StreamReader sr = new StreamReader(filepath);
string content = sr.ReadToEnd();
Console.WriteLine(content);
sr.Close();
}
else
Console.WriteLine("File not found.");
Console.Read();
}
}
}
Output:
Streams

File Writing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FileWriteDemo
{
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)
{
string content;
Console.WriteLine("\nEnter content to write:");
content = Console.ReadLine();
StreamWriter sw = new StreamWriter(filepath);
sw.Write(content);
sw.Close();
Console.WriteLine("\nWritten successfully!");
}
else
Console.WriteLine("File already exists.");
Console.Read();
}
}
}
Output:
Streams