1. Overview

The FileUtility tool is a reusable utility class designed to facilitate common file and directory operations in C# applications. It provides a set of static methods that encapsulate commonly used file handling functionality, allowing developers to perform file operations easily and efficiently.

2. Technologies and Tools Used

Below are the technologies has been used to create File Utility tool in C#.

  • Visual Studio
  • C#

3. Create FileUtility tool

1.Firstly, create Console Application in Visual Studio 2019.

2.Next add class file named FileUtility.cs.

3.Here need to include System.IO namespace for handling file functionality

4.Inside this class file, write following codes

public class FileUtility {
// Checks if a file exists at the given file path
public static bool FileExists(string filePath) {
return File.Exists(filePath);
}

// Creates a new file at the given file path if it doesn’t already exist
public static void CreateFile(string filePath) {
if (!FileExists(filePath)) {
// Using statement ensures the file is closed after creation
using(File.Create(filePath)) {}
}
}

// Deletes the file at the given file path if it exists
public static void DeleteFile(string filePath) {
if (FileExists(filePath)) {
File.Delete(filePath);
}
}

// Copies a file from the source file path to the destination file path
public static void CopyFile(string sourceFilePath, string destinationFilePath, bool overwrite = false) {
if (FileExists(sourceFilePath)) {
// Copies the file and optionally overwrites the destination file if it already exists
File.Copy(sourceFilePath, destinationFilePath, overwrite);
}
}

// Moves a file from the source file path to the destination file path
public static void MoveFile(string sourceFilePath, string destinationFilePath, bool overwrite = false) {
if (FileExists(sourceFilePath)) {
if (FileExists(destinationFilePath)) {
if (overwrite) {
// If overwrite is allowed, delete the destination file and move the source file there
File.Delete(destinationFilePath);
File.Move(sourceFilePath, destinationFilePath);
} else {
// Throw an exception if overwrite is not allowed and a file already exists at the destination path
throw new IOException($”A file already exists at the destination path: {destinationFilePath}”);
}
} else {
// Move the source file to the destination path
File.Move(sourceFilePath, destinationFilePath);
}
} else {
// Throw an exception if the source file doesn’t exist
throw new FileNotFoundException($”Source file not found: {sourceFilePath}”);
}
}

// Reads the contents of a text file at the given file path
public static string ReadTextFile(string filePath) {
if (FileExists(filePath)) {
// Reads all text from the file and returns it as a string
return File.ReadAllText(filePath);
} else {
// Throw an exception if the file doesn’t exist
throw new FileNotFoundException($”File not found: {filePath}”);
}
}

// Writes the given content to a text file at the given file path (overwriting any existing content)
public static void WriteTextFile(string filePath, string content) {
// Writes the content to the file
File.WriteAllText(filePath, content);
}

// Appends the given content to a text file at the given file path
public static void AppendTextFile(string filePath, string content) {
// Appends the content to the file
File.AppendAllText(filePath, content);
}

// Creates a new directory at the given directory path if it doesn’t already exist
public static void CreateDirectory(string directoryPath) {
if (!Directory.Exists(directoryPath)) {
// Creates the directory
Directory.CreateDirectory(directoryPath);
}
}

// Checks if a directory exists at the given directory path
public static bool DirectoryExists(string directoryPath) {
return Directory.Exists(directoryPath);
}

// Deletes a directory at the given directory path (and its contents) if it exists
public static void DeleteDirectory(string directoryPath, bool recursive = true) {
if (DirectoryExists(directoryPath)) {
// Deletes the directory and its contents recursively if the ‘recursive’ parameter is true
Directory.Delete(directoryPath, recursive);
}
}
}

5.Now we can able to use this tool in any other c# applications. Below I have provided about how to use this tool in the application.

 

4. Conclusion

The FileUtility tool can be easily integrated into any C# application, enabling developers to perform file and directory operations with minimal effort. The tool offers a simple and intuitive API that promotes code reusability and maintainability.

 

Recommended Posts

Start typing and press Enter to search