File handling in C# is the process of working with files and directories on the file system. C# provides several classes in the System.IO namespace that allow you to read from and write to files, create and delete files and directories, and perform other file-related operations.
Here’s an example of how to read the contents of a file in C#:
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string path = @"C:\example.txt";
try
{
using (StreamReader sr = new StreamReader(path))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
In this example, we first define a string variable called “path” that contains the path to the file we want to read. We then use a try-catch block to open the file using a StreamReader object, read the contents of the file line by line using the ReadLine() method, and write each line to the console using the Console.WriteLine() method.
Here’s an example of how to write to a file in C#:
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string path = @"C:\example.txt";
string[] lines = { "Hello", "world", "!" };
try
{
using (StreamWriter sw = new StreamWriter(path))
{
foreach (string line in lines)
{
sw.WriteLine(line);
}
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be written:");
Console.WriteLine(e.Message);
}
}
}
In this example, we first define a string array called “lines” that contains the lines we want to write to the file. We then use a try-catch block to open the file using a StreamWriter object, write each line to the file using the WriteLine() method, and close the file using the Dispose() method.
File handling in C# also allows you to create and delete files and directories, move and copy files, and perform other file-related operations. The System.IO namespace provides a wide range of classes and methods that allow you to work with files and directories in a flexible and powerful way.
Here’s an example of how to create and delete a file in C#:
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string path = @"C:\example.txt";
// Create a new file
try
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("This is a test file.");
}
Console.WriteLine("File created successfully.");
}
catch (Exception e)
{
Console.WriteLine("Error creating file:"); Console.WriteLine(e.Message);
}
// Delete the file
try
{
File.Delete(path);
Console.WriteLine("File deleted successfully.");
}
catch (Exception e)
{
Console.WriteLine("Error deleting file:");
Console.WriteLine(e.Message);
}
}
}
Here’s an example of how to create and delete a file in C#:
using System; using System.IO; class Program { static void Main(string[] args) { string path = @”C:\example.txt”; // Create a new file try { using (StreamWriter sw = File.CreateText(path)) { sw.WriteLine(“This is a test file.”); } Console.WriteLine(“File created successfully.”); } catch (Exception e) { Console.WriteLine(“Error creating file:”); Console.WriteLine(e.Message); } // Delete the file try { File.Delete(path); Console.WriteLine(“File deleted successfully.”); } catch (Exception e) { Console.WriteLine(“Error deleting file:”); Console.WriteLine(e.Message); } } }
In this example, we first define a string variable called “path” that contains the path to the file we want to create and delete. We then use a try-catch block to create a new file using the File.CreateText() method and write a line of text to the file using the StreamWriter object. Finally, we use the File.Delete() method to delete the file, again using a try-catch block to handle any errors that may occur.
When you run this program, it will create a new file at the specified path and write the line “This is a test file.” to it. It will then delete the file and display a message indicating whether the operation was successful or if an error occurred.
Here’s an example of how to move and copy a file in C#:
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string sourcePath = @"C:\example.txt";
string destPath = @"C:\Documents\example.txt";
// Move the file
try
{
File.Move(sourcePath, destPath);
Console.WriteLine("File moved successfully.");
}
catch (Exception e)
{
Console.WriteLine("Error moving file:"); Console.WriteLine(e.Message);
}
// Copy the file
try
{
File.Copy(destPath, @"C:\Backups\example_backup.txt");
Console.WriteLine("File copied successfully.");
}
catch (Exception e)
{
Console.WriteLine("Error copying file:");
Console.WriteLine(e.Message);
}
}
}
In this example, we first define two string variables called “sourcePath” and “destPath” that contain the paths of the source file and destination file respectively. We then use a try-catch block to move the file from the source path to the destination path using the File.Move() method. We then use another try-catch block to make a backup copy of the destination file to a new path using the File.Copy() method.
When you run this program, it will move the file from the source path to the destination path and display a message indicating whether the operation was successful or if an error occurred. It will then make a backup copy of the destination file to a new path and display another message indicating whether the operation was successful or if an error occurred.
Learners TV is a website that is designed to educate users and provide instructional material on particular subjects and topics.