Introduction
File handling in C# is a crucial aspect of many applications, providing the capability to create, read, write, and append files. This guide will explore the fundamental file handling operations in C#, offering practical examples for each operation to ensure you grasp the concepts effectively.
1. Creating a File in C#
Creating a file in C# involves using the File
class from the System.IO
namespace. This operation checks if the file exists and, if not, creates a new file.
Example: Creating a File
using System;
using System.IO;
class Program
{
static void Main()
{
string path = "example.txt";
// Check if file already exists. If yes, delete it.
if (File.Exists(path))
{
File.Delete(path);
}
// Create a new file
using (FileStream fs = File.Create(path))
{
Console.WriteLine("File created successfully.");
}
}
}
2. Writing to a File in C#
Writing to a file can be achieved using the StreamWriter
class. This allows you to write text data to a file efficiently.
Example: Writing to a File
using System;
using System.IO;
class Program
{
static void Main()
{
string path = "example.txt";
// Create a new file and write some text to it
using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine("Hello, World!");
sw.WriteLine("Welcome to file handling in C#.");
Console.WriteLine("Text written to file successfully.");
}
}
}
3. Reading from a File in C#
Reading from a file involves using the StreamReader
class, which reads text data from a file and allows you to process it line by line or as a whole.
Example: Reading from a File
using System;
using System.IO;
class Program
{
static void Main()
{
string path = "example.txt";
// Check if file exists before attempting to read
if (File.Exists(path))
{
using (StreamReader sr = new StreamReader(path))
{
string content = sr.ReadToEnd();
Console.WriteLine("File Content:");
Console.WriteLine(content);
}
}
else
{
Console.WriteLine("File does not exist.");
}
}
}
4. Appending to a File in C#
Appending to a file is done using the StreamWriter
class with the append parameter set to true
. This ensures that new data is added to the end of the file without overwriting existing content.
Example: Appending to a File
using System;
using System.IO;
class Program
{
static void Main()
{
string path = "example.txt";
// Append text to an existing file
using (StreamWriter sw = new StreamWriter(path, true))
{
sw.WriteLine("Appending new text to the file.");
Console.WriteLine("Text appended to file successfully.");
}
}
}
Operation on File:-
1) Write File:-
class FileHandlingOperation
{
public static void Main()
{
File.WriteAllText("D://mydemo.txt", "Welcome in File Handling");
Console.ReadKey();
}
}
2) Read File
class FileHandlingOperation
{
public static void Main()
{
String s= File.ReadAllText("D://mydemo.txt");
Console.WriteLine(s);
Console.ReadKey();
}
}
3) Append File Operation:-
class FileHandlingOperation
{
public static void Main()
{
File.AppendAllText("D://mydemo.txt", "Welcome in File Handling");
Console.ReadKey();
}
}
Conclusion
File handling in C# is a fundamental aspect of software development that allows you to perform various operations on files, such as creating, reading, writing, and appending. By understanding and utilizing the File
, FileStream
, StreamWriter
, and StreamReader
classes, you can effectively manage file operations in your applications.
Here’s a summary of the key operations covered:
Creating a File: Using
File.Create
.Writing to a File: Using
StreamWriter
.Reading from a File: Using
StreamReader
.Appending to a File: Using
StreamWriter
with append parameter.
POST Answer of Questions and ASK to Doubt