C# Programs on File Handling

C# Programming Examples - File

A file is a collection of data and information and a Directory is a location in the computer where files are stored. The word File Handling refers to the various operations such as creating files, reading files, writing to files, and appending the file. Reading and writing files are two basic operations that are commonly used in file handling. In C#, the FileStream class provides a stream for file operations, allowing us to easily read and write data to files. A stream is a collection of bytes that are used to communicate.

File Handling Classes:

Here are some commonly used classes in the System.IO namespace:

  • FileStream: It is a class used for reading and writing files.
  • StreamReader: It is used to read characters from a Stream.
  • StreamWriter: It is a class that writes characters in a particular encoding to a stream.
  • BinaryReader: It is used to read binary information from the stream.
  • BinaryWriter: It is used to write binary information into the stream.
  • StringReader: It is used to read data from a string buffer.
  • StringWriter: It is used to write data to a StringBuilder class.
  • FileInfo: It is used to perform file operations.
  • DirectoryInfo: It is used to perform operations on directories.

StreamWriter Methods:

  • Close() – It closes the underlying stream and the current StreamWriter object.
  • Flush() – It clears all buffers for the current writer.
  • Dispose() – It releases all resources used by the TextWriter object.
  • Equals() – It checks if the specified object is equal to the current object.

StreamReader Methods:

advertisement
advertisement
  • Close() – It closes the underlying stream and the current StreamReader object.
  • Peek() – It is used to return the next available character but does not consume it.
  • Read() – It reads the next character from the input stream to advance the character position.
  • ToString() – It returns a string that represents the current object.

The following section contains C# programs on files, directories, file operations, and System.IO namespace classes like StringReader, StringBuilder, StringWriter, BinaryReader, Memory Stream, StreamReader, and StreamWriter. Every example program includes the problem description, problem solution, C# code, program explanation, and run-time test cases. All C# examples have been compiled and tested on Visual Studio.

Here is the listing of C# programming examples on File Handling:

  1. C# Programs on Files and Directories
  2. C# Programs on File Operations
  3. C# Programs on StringReader, StringBuilder and BinaryReader
  4. C# Programs on StringWriter and Memory Stream
  5. C# Programs on StreamReader and StreamWriter

1. C# Programs on Files and Directories

Program Description
Create a New File in C# C# Program to Create a File
Create Directory in C# C# Program to Create a Directory
Program to Read File Content in C# C# Program to Read a Text File
C# Program to Check Information of the File C# Program to Check the Information of the File
View the Access Date and Time of a File in C# C# Program to View the Access Date and Time of a File
Program to Search Directories and List Files in C# C# Program to Search Directories and List Files
Check if File Exists in C# C# Program to Check if File Exists or Not
List Files in Directory in C# C# Program to List Files in Directory

2. C# Programs on File Operations

Program Description
Copy One File to Another File in C# C# Program to Copy the Contents of One File to Another File
File Comparison in C# C# Program to Compare Two Text Files
Calculate Directory Size in C# C# Program to Calculate the Size of Folder
Get File Time using File Class in C# C# Program to Get File Time using File Class
Trap Events from File in C# C# Program to Trap Events from File
List Disk Drives in C# C# Program to List All Available Disk Drives

3. C# Programs on StringReader, StringBuilder and BinaryReader

Program Description
StringReader in C# C# Program to Demonstrate StringReader
StringBuilder in C# C# Program to Demonstrate StringBuilder
Binary Reader in C# C# Program to Demonstrate BinaryReader
Binary Writer in C# C# Program to Demonstrate BinaryWriter

advertisement

4. C# Programs on StringWriter and Memory Stream

Program Description
StringWriter in C# C# Program to Demonstrate StringWriter
Memory Stream Class in C# C# Program to Demonstrate Memory Stream Class
Convert Stream to Chars in C# C# Program to Read Data from Stream and Convert Data to Chars
Read Bytes from File in C# C# Program to Get Content from a File and Read the Content 1 Byte at a Time

5. C# Programs on StreamReader and StreamWriter

Program Description
Read All Line using StreamReader in C# C# Program to Read All Lines using StreamReader
File Operations in C# C# Program to Perform File Operations
Read a File Line by Line in C# C# Program to Read Lines from a File until the End of File is Reached
Get File Time using File Class in C# C# Program to Get File Time using File Class
FileInfo Class in C# C# Program to Illustrate Methods of FileInfo Class

advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.