Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. IO;
Namespace WinFormsApp_OperateFileAndFolder
{
Public class OperateFileFolder
{
/// <Summary>
/// Create a folder
/// </Summary>
Public string CreateFolder (string argPath)
{
String returnStr = "";
Try
{
If (System. IO. Directory. Exists (argPath ))
{
ReturnStr = "this folder already exists ";
Return returnStr;
}
Else
{
System. IO. DirectoryInfo dirinfo = System. IO. Directory. CreateDirectory (argPath );
ReturnStr = "this folder is created successfully! Creation Time: "+ System. IO. Directory. GetCreationTime (argPath );
}
}
Catch (Exception ee)
{
ReturnStr = "processing failed! Cause of failure: "+ ee. ToString ();
}
Return returnStr;
}
/// <Summary>
/// Recursively delete folders and files
/// </Summary>
/// <Param name = "dir"> </param>
Public void DeleteFolder (string dir)
{
// Cancel blocking. After this method is executed, you can keep the root folder (only delete all the sub-folders in the directory)
//// Check whether the target directory ends with a directory delimiter. If not, add it.
// If (dir [dir. Length-1]! = Path. DirectorySeparatorChar)
// Dir + = Path. DirectorySeparatorChar;
If (Directory. Exists (dir) // if this folder Exists, delete it
{
Foreach (string d in Directory. GetFileSystemEntries (dir ))
{
If (File. Exists (d ))
File. Delete (d); // Delete the File directly.
Else
DeleteFolder (d); // recursively Delete subfolders
}
Directory. Delete (dir); // Delete an empty folder
Console. Write (dir + "folder deleted successfully ");
}
Else
Console. Write (dir + "this folder does not exist"); // If the folder does not exist, a prompt is displayed.
}
/// <Summary>
/// Implement a static method to copy all the content under the specified folder to the target folder
/// If the target folder is read-only, an error is returned.
/// </Summary>
Public static void CopyDir (string srcPath, string aimPath)
{
Try
{
// Check whether the target directory ends with a directory delimiter. If not, add it.
If (aimPath [aimPath. Length-1]! = Path. DirectorySeparatorChar)
AimPath + = Path. DirectorySeparatorChar;
// Determine whether the target directory exists. If it does not exist, create it.
If (! Directory. Exists (aimPath) Directory. CreateDirectory (aimPath );
// Obtain the file list of the source Directory, which is an array containing the file and directory path
// Use the following method if you direct to the file under the copy target file without including the Directory
// String [] fileList = Directory. GetFiles (srcPath );
String [] fileList = Directory. GetFileSystemEntries (srcPath );
// Traverse all files and directories
Foreach (string file in fileList)
{
// Process the file as a directory first. If this directory exists, recursively Copy the file under this directory.
If (Directory. Exists (file ))
CopyDir (file, aimPath + Path. GetFileName (file ));
// Otherwise, Copy the file directly.
Else
File. Copy (file, aimPath + Path. GetFileName (file), true );
}
}
Catch (Exception e)
{
System. Windows. Forms. MessageBox. Show (e. ToString ());
}
}
}
}