System.IO provides all the required classes, methods and properties for manipulating with Files and Directories. C#.net provides synchronous and a synchronous read/write operation using the following namespaces.
System.IO.FileSystemInfo is the Base class for File System operations. FileSystemInfo includes members like Delete (Delete File or Directory), GetHashCode, GetLifeTimeService, GetType ,Refresh, ToString, FullPath and OriginalPath.
Sysntem.IO.DirectoryInfo provides methods for creating, moving and deleting directories. DirectoryInfo includes members like Create, CreateSubDirecory, Delete, GetAccessControl,GetDirectories, GetFiles, GetFileSystemInfos, MoveTo,Refresh, SetAccessControl,Atributes, Exists, FullName, LastAccess Details, Parent and Name
System.IO.FileInfo provides methods for creating, appending, opening, moving and deleting files. FileInfo includes members like AppendText, CopyTo, Create,Decrypt , Delete, Encrypt, Finalize, GetAccessControl, MoveTo, Open, OpenRead, OpenText, OpenWrite, Refresh, SetAccessControl, Atributes, Exists, FullName, LastAccess Details and IsReadOnly.
System.IO.Stream Provides generic view of the Sequence of byte. Mainly used with File manipulation operations.
How to Read Text from File:
Following is the code sample to read the text from file using the ReadAllLines or ReadAllText
using System;
using System.IO;
namespace NinadBlog.Examples
{
class FileRead
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
using (StreamReader srReadFile = new StreamReader("NinadBlogFileTest.txt"))
{
String strLine;
// Read and display lines from the file until the end of
while ((strLine = srReadFile.ReadLine()) != null)
{
Console.WriteLine(strLine);
}
}
}
catch (Exception ex)
{
Console.WriteLine("The file could not be read: " & ex.Message);
}
}
}
}
How to write Text File:
Following is the code sample to write text in the file using the WriteAll.
using System;
using System.IO;
namespace NinadBlog.Examples
{
class FileWrite
{
public static void Main()
{
try
{
// Create an instance of StreamWriter to write in a file.
using (StreamWriter swWriter = new StreamWriter("NinadBlogFileTest.txt"))
{
// Add some text to the file.
sw.Write("This is the WriteStream Example on Ninad CS blog.");
sw.WriteLine("-------------------");
sw.WriteLine("By Ninad Pradeep Pandit");
sw.close();
}
}
}
catch (Exception ex)
{
Console.WriteLine("The file write Exception: " & ex.Message);
}
}
}
}
Best Practices
1. If you are going to reuse an object several times, consider using the instance method of DirectoryInfo/FileInfo or Syste instead of the corresponding static methods of the System.IO.Directory/File, to avoid un-necessary security check every time.
2. Make sure we give correct access to file after creation, By default, full read/write access is granted to all users.
3. Do not override the Close method for Stream. Close method, instead, put all of the Stream cleanup logic in the Dispose method. For more information