As programmers, one of the tasks that we frequently need to address is the manipulation of files and directories. Creating, moving, copying, and deleting files and directories are common tasks in programming or task automation.
While the .NET Framework provides us with an enormous library of functions for manipulating files and directories, these are basic functions designed for us to build and assemble our own programs.
If we want to do more complex tasks, such as copying an entire directory with all its subfolders and directories, we will have to program our own functions.
This time, I share with you a library of functions to manipulate files and directories in C#. The functions are still simple, and you will likely need to modify them according to your needs. But they may save you time, or serve as a starting point for your own functions.
Among the available functions are:
- Create an empty file
- Create an empty directory
- Delete a file
- Delete the contents of a directory
- Delete a directory and its contents
- Copy a file
- Copy the contents of a directory
- Copy a directory and its contents
The function for copying a file allows indicating whether we want to overwrite the file at the destination.
Similarly, the function to copy a directory and its contents allows indicating whether to completely replace the destination directory, or only replace the existing files in the source directory.
All functions handle files marked as read-only.
The functions work correctly without the need for any changes. However, in some cases, you will have to modify them according to your needs, and adapt them to your exception handling system. You are free to use them as a base and modify them as needed.
You are free to use this code in any application you want, both open source and commercial. Of course, we are not responsible for any damage or cause of information that the use of this code may generate.
You are free to use this code in any application you want, both open source and commercial. Of course, we are not responsible for any damage or cause of information that the use of this code may generate.
using System;
//using System.IO;
namespace FileLibrary
{
class FileLibrary
{
/// <summary>
/// Create an empty file
/// </summary>
public static void CreateEmptyFile(string fullPath)
{
if (!System.IO.File.Exists(fullPath))
{
using (System.IO.File.Create(fullPath)) { }
}
}
/// <summary>
/// Create an empty directory
/// </summary>
public static void CreateEmptyDirectory(string fullPath)
{
if (!System.IO.Directory.Exists(fullPath))
{
System.IO.Directory.CreateDirectory(fullPath);
}
}
/// <summary>
/// Delete a file
/// </summary>
public static void DeleteFile(string fullPath)
{
if (System.IO.File.Exists(fullPath))
{
System.IO.FileInfo info = new System.IO.FileInfo(fullPath);
info.Attributes = System.IO.FileAttributes.Normal;
System.IO.File.Delete(fullPath);
}
}
/// <summary>
/// Delete a directory and its contents
/// </summary>
public static void DeleteFolder(string fullPath)
{
if (System.IO.Directory.Exists(fullPath))
{
System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(fullPath) {
Attributes = System.IO.FileAttributes.Normal
};
foreach (var info in directory.GetFileSystemInfos("*", System.IO.SearchOption.AllDirectories))
{
System.IO.FileInfo fInfo = info as System.IO.FileInfo;
if (fInfo != null) info.Attributes = System.IO.FileAttributes.Normal;
}
System.Threading.Thread.Sleep(100);
directory.Delete(true);
}
}
/// <summary>
/// Delete the contents of a directory
/// </summary>
public static void DeleteFolderContent(string fullPath)
{
DeleteFolder(fullPath);
CreateEmptyDirectory(fullPath);
}
/// <summary>
/// Copy file
/// </summary>
public static void CopyFile(string origPath, string destPath, bool overwrite)
{
try
{
if (System.IO.Path.GetExtension(destPath) == "")
{
destPath = System.IO.Path.Combine(destPath, System.IO.Path.GetFileName(origPath));
}
if(!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(destPath)))
{
CreateEmptyDirectory(System.IO.Path.GetDirectoryName(destPath));
}
if (!System.IO.File.Exists(destPath))
{
System.IO.File.Copy(origPath, destPath, true);
}
else
{
if (overwrite == true)
{
DeleteFile(destPath);
System.IO.File.Copy(origPath, destPath, true);
}
}
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Copy the contents of a directory
/// </summary>
private static void CopyDirectoryContent(string origPath, string destPath, bool overwrite)
{
if (System.IO.Directory.Exists(origPath))
{
foreach (string dirPath in System.IO.Directory.GetDirectories(origPath, "*", System.IO.SearchOption.AllDirectories))
{
CreateEmptyDirectory(dirPath.Replace(origPath, destPath));
}
foreach (string newPath in System.IO.Directory.GetFiles(origPath, "*.*", System.IO.SearchOption.AllDirectories))
{
CopyFile(newPath, newPath.Replace(origPath, destPath), overwrite);
}
}
}
/// <summary>
/// Copy a directory and its contents
/// </summary>
public static void CopyDirectory(string origPath, string destPath, bool replace)
{
if (replace == true)
{
DeleteFolder(destPath);
CreateEmptyDirectory(destPath);
CopyDirectoryContent(origPath, destPath, true);
}
else
{
CopyDirectoryContent(origPath, destPath, true);
}
}
}
}