C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
The FileInfo
class in C# is part of the System.IO
namespace and provides methods and properties to perform various operations on files, such as reading, writing, and getting information about the file. In this tutorial, we'll go over some common file operations using the FileInfo
class.
Before using the FileInfo
class, make sure to include the following directive at the top of your file:
using System.IO;
To create a new instance of the FileInfo
class, pass the file path as a parameter to the constructor:
string filePath = "example.txt"; FileInfo fileInfo = new FileInfo(filePath);
To create a new file, use the Create
method. If the file already exists, it will be overwritten:
using (FileStream fileStream = fileInfo.Create()) { // Do something with the file stream, e.g., write data to the file }
To write text to a file, first create a StreamWriter
and then call its Write
or WriteLine
methods:
string content = "Hello, World!"; using (StreamWriter writer = fileInfo.CreateText()) { writer.WriteLine(content); }
To append text to an existing file, first create an StreamWriter
with the AppendText
method, and then call its Write
or WriteLine
methods:
string content = "This is an appended line."; using (StreamWriter writer = fileInfo.AppendText()) { writer.WriteLine(content); }
To read the contents of a file as a string, use the StreamReader
class:
using (StreamReader reader = fileInfo.OpenText()) { string content = reader.ReadToEnd(); Console.WriteLine("File content:"); Console.WriteLine(content); }
To check if a file exists, use the Exists
property:
if (fileInfo.Exists) { Console.WriteLine("The file exists."); } else { Console.WriteLine("The file does not exist."); }
To delete a file, use the Delete
method:
if (fileInfo.Exists) { fileInfo.Delete(); Console.WriteLine("File deleted."); } else { Console.WriteLine("The file does not exist."); }
To move a file to a new location or rename it, use the MoveTo
method:
string destinationFilePath = "example_renamed.txt"; fileInfo.MoveTo(destinationFilePath);
The FileInfo
class provides various properties to get information about the file:
Name
: Gets the file name.Length
: Gets the file size in bytes.DirectoryName
: Gets the directory where the file is located.Extension
: Gets the file extension.CreationTime
: Gets or sets the creation time of the file.LastAccessTime
: Gets or sets the last access time of the file.LastWriteTime
: Gets or sets the last write time of the file.In this tutorial, we've covered some common file operations using the FileInfo
class in C#. By using the methods and properties provided by the FileInfo
class, you can easily create, read, update, and delete files, as well as get information about the file in your C# applications.
Working with files using C# FileInfo:
FileInfo
class is part of the System.IO
namespace and allows you to perform file-related operations in an object-oriented manner.using System; using System.IO; class Program { static void Main() { string filePath = "example.txt"; // Create a FileInfo object FileInfo fileInfo = new FileInfo(filePath); // Check if the file exists if (fileInfo.Exists) { // Perform file operations using FileInfo Console.WriteLine($"File '{fileInfo.FullName}' exists!"); } else { Console.WriteLine($"File '{fileInfo.FullName}' does not exist."); } } }
Reading and writing files with FileInfo in C#:
FileInfo.OpenText
for reading and FileInfo.CreateText
for writing text files.using System; using System.IO; class Program { static void Main() { string filePath = "example.txt"; // Create a FileInfo object FileInfo fileInfo = new FileInfo(filePath); // Writing to a file using (StreamWriter writer = fileInfo.CreateText()) { writer.WriteLine("Hello, FileInfo!"); } // Reading from a file using (StreamReader reader = fileInfo.OpenText()) { string content = reader.ReadToEnd(); Console.WriteLine($"File content: {content}"); } } }
Appending to a file with FileInfo in C#:
FileInfo.AppendText
to append content to an existing file using FileInfo
.using System; using System.IO; class Program { static void Main() { string filePath = "example.txt"; // Create a FileInfo object FileInfo fileInfo = new FileInfo(filePath); // Append to a file using (StreamWriter writer = fileInfo.AppendText()) { writer.WriteLine("\nAppended content with FileInfo."); } // Reading from the file after appending using (StreamReader reader = fileInfo.OpenText()) { string content = reader.ReadToEnd(); Console.WriteLine($"File content after appending: {content}"); } } }
Deleting files using FileInfo in C#:
FileInfo.Delete
method to delete a file using FileInfo
.using System; using System.IO; class Program { static void Main() { string filePath = "example.txt"; // Create a FileInfo object FileInfo fileInfo = new FileInfo(filePath); // Delete the file fileInfo.Delete(); Console.WriteLine($"File '{fileInfo.FullName}' deleted."); } }
Renaming files with C# FileInfo class:
FileInfo.MoveTo
method to rename or move a file using FileInfo
.using System; using System.IO; class Program { static void Main() { string oldFilePath = "old.txt"; string newFilePath = "new.txt"; // Create FileInfo objects FileInfo oldFileInfo = new FileInfo(oldFilePath); FileInfo newFileInfo = new FileInfo(newFilePath); // Rename the file oldFileInfo.MoveTo(newFileInfo.FullName); Console.WriteLine($"File renamed from '{oldFileInfo.FullName}' to '{newFileInfo.FullName}'."); } }