Classes | Description |
---|---|
File | Provides the basic functionality for reading and writing. |
Directory | Used copying, moving, renaming, creating, and deleting directories. |
Path | Path class methods enables you to determine the location of file or directory |
FileInfo | Mainly used for access and manipulating the files. |
DirectoryInfo | It provides the information about the particular directory. |
FileSystemInfo | It represents either a file or a directory. |
FileStream | Provides read, write facility to the files. |
StreamReader | It is used to read data from files. |
StreamWriter | It enables you to write data to file. |
FileSystemWatche | It monitors changes to subdirectories and files within the specified directory. |
using System;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
FileInfo myFile = new FileInfo(@"c:\myDataFile.txt");
if (myFile.Exists)
{
Console.WriteLine ("Filename : {0}", myFile.Name);
Console.WriteLine ("Path : {0}", myFile.FullName);
Console.WriteLine ("DirectoryName : {0}", myFile.DirectoryName);
Console.WriteLine ("Length : {0}", myFile.Length);
myFile.CopyTo (@"D:\myDataFile.txt");
}
Console.ReadLine();
}
}
}
using System;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Create Method is used to create file.
FileInfo myFile = new FileInfo(@"c:\myDataFile.txt");
FileStream fs = myFile.Create();
Console.WriteLine("File has been created");
// CreateText Method
FileInfo fileObj = new FileInfo(@"D:\FileWithText.txt");
StreamWriter str = fileObj.CreateText();
str.WriteLine("Welcome at TutorialRide");
Console.WriteLine("File has been created with text");
str.Close();
// Delete Method of FileInfo class is used to delete file.
if (fileObj.Exists)
{
fileObj.Delete();
Console.WriteLine("File has been deleted");
}
// MoveTo Method
string path = @"C:\FileWithText.txt";
if (fileObj.Exists)
{
fileObj.MoveTo(path);
Console.WriteLine("File moved to {0}.", path);
}
//Please keep in mind, if the file with same name is already exist in
//designated path, then it will through the exception.
// ApendText Method
FileInfo fi = new FileInfo(@"D:\NewFile.txt");
StreamWriter swObj = fi.AppendText();
if (fi.Exists)
{
swObj.WriteLine("Welcome");
swObj.WriteLine("at");
swObj.WriteLine("TutorialRide");
Console.WriteLine("File has been appended");
swObj.Close();
}
//Use of Opentext Method
StreamReader readerObj = fi.OpenText();
string str = "";
while ((str = readerObj.ReadLine()) != null)
{
Console.WriteLine(str);
}
Console.ReadLine();
}
}
}
using System;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo myDir = new DirectoryInfo(@"c:\windows");
Console.WriteLine("FullName: {0}", myDir.FullName);
Console.WriteLine("Name: {0}", myDir.Name);
Console.WriteLine("CreationTime: {0}", myDir.CreationTime);
Console.WriteLine("LastAccessTime: {0}", myDir.LastAccessTime);
Console.WriteLine("LastWriteTime: {0}", myDir.LastWriteTime);
Console.WriteLine("Root: {0}", myDir.Root);
Console.ReadLine();
}
}
}
using System;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Create Method
String path = @"D:\MyDirectory";
DirectoryInfo dirObj = new DirectoryInfo(path);
dirObj.Create();
if (dirObj.Exists)
{
Console.WriteLine("Directory has been created");
}
// CreateSubdirectory Method
dirObj.CreateSubdirectory("MySubDirectory");
Console.WriteLine("Sub Directory has been created");
// MoveTo Method
dirObj.MoveTo(@"D:\MyNewDirectory");
Console.WriteLine("Directory has been Moved within drive");
// you can move directive in same drive
//// Delete Method
if (dirObj.Exists)
{
dirObj.Delete();
Console.WriteLine("Directory has been deleted");
}
// Get directories and files in particular Folder.
dirObj = new DirectoryInfo(@"C:\WINDOWS");
foreach (FileInfo file in dirObj.GetFiles())
{
Console.WriteLine("File: {0}", file.Name);
}
Console.WriteLine("\n=======================================\n");
foreach (DirectoryInfo file in dirObj.GetDirectories())
{
Console.WriteLine("Direcotory: {0}", file.Name);
}
Console.ReadLine();
}
}
}
FileMode | Description |
---|---|
CreateNew | It creates the new file. |
Append | Opens the file if exists, if not create new file and append the text. |
Create | Creates the new file, if file exists it will be overwritten. |
Open | It is used for open an existing file. |
OpenOrCreate | Opens the file if exits otherwise it creates new file. |
Truncate | It is used to truncate the content. |
using System;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
StreamWriter sw = null;
try
{
FileStream fileStream = new FileStream(@"D:\MyFile.txt", FileMode.OpenOrCreate);
sw = new StreamWriter(fileStream);
// Write data to file.
sw.WriteLine("Welcome at TutorialRide.");
sw.Write("C# is fun.");
}
catch (IOException e)
{
Console.WriteLine("An IO exception has occured!");
Console.WriteLine(e.Message);
}
finally
{
sw.Close();
}
Console.ReadLine();
}
}
}
using System;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str;
FileStream fileStream=null;
StreamReader sr = null;
try
{
fileStream = new FileStream(@"D:\MyFile.txt",FileMode.Open);
sr = new StreamReader(fileStream);
str = sr.ReadLine();
// Read data in line by line.
while (str != null)
{
Console.WriteLine(str);
str = sr.ReadLine();
}
}
catch (IOException e)
{
Console.WriteLine("An IO exception has been occured!");
Console.WriteLine(e.Message);
}
finally
{
sr.Close();
}
Console.ReadLine();
}
}
}
Enumeration | Value |
---|---|
FileMode | Create, CreateNew, Append, Open, OpenOrCreate, Truncate |
FileAccess | Read, Write, ReadWrite |
FileShare | Read, Write, ReadWrite,None, Delete, Truncate, Inheritable |
using System;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string path = @"D:\MyDataFile.txt";
// Write information to the file.
StreamWriter sw = File.CreateText(path);
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome at TutorialRide");
sw.Close();
// Open the file to read.
StreamReader sr = File.OpenText(path);
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
sr.Close();
Console.ReadLine();
}
}
}
using System;
using System.IO;
using System.IO.Compression;
namespace ConsoleGZIPSTREAM
{
class Program
{
static void SaveFile(string filename, string data)
{
FileStream fileStream =
new FileStream(filename, FileMode.Create, FileAccess.Write);
GZipStream zipStream =
new GZipStream(fileStream, CompressionMode.Compress);
StreamWriter sw = new StreamWriter(zipStream);
sw.Write(data);
sw.Close();
}
static string LoadCompressedFile(string filename)
{
FileStream fileStream =
new FileStream(filename, FileMode.Open, FileAccess.Read);
GZipStream zipStream =
new GZipStream(fileStream, CompressionMode.Decompress);
StreamReader sr = new StreamReader(zipStream);
string data = sr.ReadToEnd();
sr.Close();
return data;
}
static void Main(string[] args)
{
Console.WriteLine("Enter you information for compression");
string str = Console.ReadLine();
SaveFile(@"D:\MyDataFile.txt",str);
string result = LoadCompressedFile(@"D:\MyDataFile.txt");
Console.WriteLine(result);
Console.ReadLine();
}
}
}