C implements the method of copying files in a folder to another folder
- 2021-07-13 06:06:58
- OfStack
This article illustrates how C # implements copying files from a folder to another folder. Share it for your reference. The details are as follows:
private void CopyDir(string srcPath, string aimPath)
{
try
{
// Check whether the destination directory ends with a directory dividing character. If not, add
if (aimPath[aimPath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
{
aimPath += System.IO.Path.DirectorySeparatorChar;
}
// Determine whether the target directory exists. If it does not exist, create a new one
if (!System.IO.Directory.Exists(aimPath))
{
System.IO.Directory.CreateDirectory(aimPath);
}
// Get the file list of the source directory, which contains the file and directory path 1 Array of numbers
// If you point to copy Use the following method for files below the target file without directories
// string[] fileList = Directory.GetFiles ( srcPath );
string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath);
// Traverse all files and directories
foreach (string file in fileList)
{
// Treat it as a directory first. If this directory exists, recursive Copy Files under this directory
if(System.IO.Directory.Exists(file))
{
CopyDir(file, aimPath + System.IO.Path.GetFileName(file));
}
// Otherwise directly Copy Documents
else
{
System.IO.File.Copy(file, aimPath + System.IO.Path.GetFileName(file),true);
}
}
}
catch(Exception e)
{
throw;
}
}
}
I hope this article is helpful to everyone's C # programming.