File Handling in C#

File handling in C# allows you to read, write, and manipulate data stored in files. In this section, you'll learn how to work with different types of files, such as text files, binary files, and more. You'll explore how to open, close, and manage file streams, as well as how to handle exceptions when dealing with file operations. Mastering file handling in C# is essential for building applications that interact with external data and save important information.



  • Program in C# to Store Sentences in a Text File

    This program will ask the user to input multiple sentences. Each sentence will be stored in a text file named "sentences.txt". The program will keep asking for new sentences until ...

  • Program in C# to Append Sentences to a Text File

    This program prompts the user to input several sentences and stores them in a text file named "sentences.txt". If the file already exists, the new sentences will be appended to the...

  • Program in C# to Display the Contents of a Text File

    This program reads and displays the contents of a specified text file on the screen. The name of the file can either be provided as a command line argument or, if no argument is gi...

  • Convert Text to HTML and Dump to File in C#

    This program implements a class TextToHtml that can convert several user-entered lines of text into an HTML sequence. The program will have methods to add text to an array, display...

  • Logger Class to Append Text with Date and Time in C#

    This program creates a Logger class with a static method Write. The Write method appends a specified text message to a log file, including the current date and time before the mess...

  • Simulating Unix 'More' Command in C#

    Create a C# program that mimics the functionality of the Unix command more, which allows the user to view content from a text file one screen at a time. The program will display 24...

  • Word Replacement Program in C#

    This program is designed to replace all occurrences of a specific word in a text file and save the result into a new file. The user provides the original file, the word to search f...

  • Character Count Program from file in C#

    This program counts the number of times a specified character appears in a text file. The program can either ask the user for the file and the character to search or accept them as...

  • BMP Image File Validator in C#

    This program checks if a BMP image file seems to be correct by verifying its header. Specifically, it checks if the first two bytes of the file match the ASCII codes for the letter...

  • Store and Read Personal Data in a Binary File in C#

    This program asks the user for their name, age (as a byte), and birth year (as an int). The data is then stored in a binary file. The program will also include a reader to test tha...

  • Basic C# to Java Source Code Coverter

    This program is designed to translate a simple C# source file into an equivalent Java source file. It will take a C# file as input, and generate a Java file by making common langua...

  • Reverse the Contents of a Text File in C#

    This program takes a text file as input and creates a new file with the same name, but with a ".tnv" extension. The new file will contain the same lines as the original file, but i...

  • Check and Validate GIF Image File in C#

    This program checks if a GIF image file is correctly formatted by inspecting the first few bytes of the file. It reads the first four bytes to confirm if they match the ASCII codes...

  • Persist Data in Friends Database in C#

    This program expands the "friends database" by adding functionality to load and save data from and to a file. The program will check for an existing file when the application start...

  • Pascal to C# Translator Converter

    This program is a basic Pascal-to-C# translator. It accepts a Pascal source file and converts it into an equivalent C# program. The program reads a Pascal file provided by the user...

  • Text File Uppercase Converter in C#

    This program reads the content of a text file and converts all lowercase letters to uppercase. After processing the text, the program writes the modified content to another text fi...

  • Convert Text to Uppercase and Save to New File in C#

    This program is designed to read a given text file, convert all lowercase letters to uppercase, and then save the modified content to a new file. The program will take an input fil...

  • Invert File Content and Save in Reverse Order in C#

    This program is designed to read a binary file and create a new file with the same name but with a ".inv" extension. The new file will contain the same bytes as the original file, ...

  • Text File Encryption Program in C#

    This program is designed to encrypt a text file and save the encrypted content into another text file. The encryption method used here is a simple character shifting technique, whe...

  • Word Count Program for Text File in C#

    This program is designed to read a text file and count the number of words it contains. A word is considered any sequence of characters separated by spaces, newlines, or punctuatio...

  • Display BMP File Width and Height using BinaryReader in C#

    This program is designed to read a BMP (Bitmap) image file and display its width and height. The BMP file format has a specific header structure, which contains important informati...

  • File Text to HTML Converter in C#

    This program is a "Text to HTML Converter" that reads the contents of a source text file and converts it into an HTML file. The program will take the text file and process its cont...

  • File Inverter Using FileStream in C#

    This program takes a file and creates a new file where the bytes are inverted. The program reads the contents of the original file using a FileStream, then writes these contents to...

  • Display Width and Height of a BMP File Using FileStream in C#

    This program reads the header of a BMP (Bitmap) file and extracts the width and height of the image using FileStream. It uses the structure of the BMP header, where the width and h...

  • Copy Source File to Destination File Using FileStream in C#

    This program copies a source file to a destination file using FileStream in C#. The file is read and written in blocks of 512 KB at a time to handle large files efficiently. The pr...

  • Tag Reader for Audio Files in C#

    This program reads and extracts the ID3 version 1 tags from an audio file, specifically the last 128 bytes of the file, which contain metadata information such as the title, artist...

  • Simple C to C# Converter

    This program is designed to convert simple C programs into C# code. It takes a C program as input and translates it into a corresponding C# program that compiles and runs correctly...

  • File Splitter Program in C#

    This program allows you to split a file of any type into smaller pieces of a specified size. The program takes two parameters: the name of the file to split and the size (in bytes)...

  • BMP Image File 'Encrypt-Decrypt' Program in C#

    This program allows you to encrypt and decrypt BMP image files by manipulating the "BM" mark located in the first two bytes of the file. The program swaps the "BM" marker at the be...

  • CSV to Text Converter in C#

    This program reads a CSV file that contains four data blocks per line. The first three data blocks are textual (name, surname, and city), and the last one is numeric (age). It proc...

  • File Comparison Program in C#

    This C# program compares two files of any type (text, binary, etc.) to determine if they are identical. The program reads the content of both files and checks whether every byte in...

  • Netpbm Image Decoder in C#

    This C# program decodes a Netpbm image file (specifically the "P1" type) and displays the image content in the console. The program reads the header, dimensions, and pixel data fro...

  • PCX Image File Checker in C#

    This C# program checks if a given file is a valid PCX image file. If the file is a PCX image, the program reads the file's header to extract the width and height of the image. The ...

  • Extract Alphabetic Characters from Binary File in C#

    This C# program extracts only the alphabetic characters from a binary file and dumps them into a separate file. The program identifies characters with ASCII codes between 32 and 12...

  • Convert C# Program to Pascal

    This C# program is designed to convert simple C# programs into Pascal language code. The program reads a C# source code file and translates basic constructs like if, for, while, va...

  • Hex Viewer Utility - Dump Utility in C#

    This C# program is a "dump" utility that functions as a hex viewer. It displays the contents of a given file in hexadecimal format, with 16 bytes in each row and 24 rows in each sc...

  • Display Fields in a DBF File in C#

    In this exercise, you will create a program that reads a DBF file and displays a list of fields contained within it. The DBF file format, used by the old dBase database manager, is...

  • Censor Text File Program in C#

    In this exercise, you will create a program that reads a text file, checks each word against a list of words to censor, and writes the modified text to a new file. The words to cen...

  • Extract Data from SQL INSERT Statements in C#

    In this exercise, you will create a C# program that parses SQL INSERT commands from a file and extracts their data in a structured format. The program will read SQL INSERT statemen...

  • PGM Image Parser and Console Display in C#

    This program reads a PGM image file in binary format (P5) and represents its shades of gray in the console using different characters based on the intensity value. The program firs...

  • Display a BMP Image on Console in C#

    This program reads a 72x24 BMP image file and displays it on the console. It uses the information from the BMP header to determine the start of image data and processes the pixels ...

More C# Practice Exercises Grouped

  • Getting Started with C# Programming

    Starting your journey with C# programming opens the door to a wide range of possibilities in software development. In this section, you'll learn the foundational concepts of C#—fro...

  • C# Flow Control Basics

    Mastering flow control is essential to writing efficient and dynamic programs in C#. This section covers the key structures that guide the flow of execution in your applications, s...

  • C# Basic Data Types Overview

    Understanding the basic data types in C# is crucial for building strong, efficient applications. This section introduces the fundamental building blocks of C# programming, includin...

  • C# Arrays, Structures and Strings

    Arrays, structures, and strings are fundamental data structures in C#. This section explores how to store and manage collections of data using arrays, organize complex data using s...

  • Functions in C#

    Functions are the building blocks of reusable code in C#. In this section, you'll learn how to define and use functions to organize your code, improve readability, and avoid repeti...

  • Object-Oriented Programming in C#

    Object-Oriented Programming (OOP) is a core concept in C# that allows you to model real-world problems efficiently. In this section, you'll dive into the principles of OOP, includi...

  • Advanced Classes in C#

    Advanced classes in C# go beyond basic object-oriented concepts, allowing you to create more complex and powerful structures. This section covers topics such as inheritance, interf...

  • Object Persistence in C#

    Object persistence in C# refers to the ability to store and retrieve objects from permanent storage, such as databases or files. In this section, you'll learn how to use techniques...

  • Working with Relational Databases in C#

    Working with relational databases in C# enables you to store, retrieve, and manipulate structured data efficiently. In this section, you'll explore how to connect to a relational ...

  • Dynamic Memory Management in C#

    Dynamic memory management in C# allows you to allocate, use, and release memory during runtime, optimizing resource usage and performance. In this section, you'll explore how to w...

  • Using Additional Libraries in C#

    Using additional libraries in C# allows you to extend the functionality of your applications and leverage pre-built solutions to solve common problems. In this section, you'll lear...

Maybe you will be interested in these C# Practice Exercises

  • Working with Arrays and Binary Files in C#

    This exercise focuses on working with arrays and binary files in C#. You will create a program that allows you to dump the contents of an array into a binary file and restore the d...

  • Working with Classes, Arrays, and Binary Files in C#

    This exercise focuses on working with classes, arrays, and binary files in C#. The goal is to create three classes: Table, SetOfTables, and a test program. The SetOfTables class wi...

  • Persisting Insects Data in C#

    This exercise focuses on persisting data using a storage method such as a database or file system. You will create a new version of the 'insects' exercise where the data about inse...

  • Persisting Cities Data in C# with Database

    This exercise focuses on creating a new version of the 'cities database' program. Instead of using text files, this version will utilize persistence to store the city data in a dat...