Class File_DirManipulate
{
/// <Summary>
///FileCopy
/// </Summary>
/// <Param name = "srcFilePath"> Source Path </param>
/// <Param name = "destFilePath"> target path </param>
Public static void FileCopy (string srcFilePath, string destFilePath)
{
File. Copy (srcFilePath, destFilePath );
}
/// <Summary>
///FileMove
/// </Summary>
/// <Param name = "srcFilePath"> Source Path </param>
/// <Param name = "destFilePath"> target path </param>
Public static void FileMove (string srcFilePath, string destFilePath)
{
File. Move (srcFilePath, destFilePath );
}
/// <Summary>
///FileDelete
/// </Summary>
/// <Param name = "delFilePath"> </param>
Public static void FileDelete (string delFilePath)
{
File. Delete (delFilePath );
}
/// <Summary>
///Delete contents in folders and folders
/// </Summary>
/// <Param name = "delFolderPath"> </param>
Public static void FolderDelete (string delFolderPath)
{
If (delFolderPath [delFolderPath. Length-1]! = Path. DirectorySeparatorChar)
DelFolderPath + = Path. DirectorySeparatorChar;
// String [] fileList = Directory. GetFileSystemEntries (delFolderPath );
Foreach (string item in Directory. GetFileSystemEntries (delFolderPath ))
{
If (File. Exists (item ))
{
FileInfo fi = new FileInfo (item );
If (fi. Attributes. ToString (). IndexOf ("ReadOnly ")! =-1) // change the attribute of the read-only file. Otherwise, the file cannot be deleted.
Fi. Attributes = FileAttributes. Normal;
File. Delete (item );
} // Delete the file
Else
FolderDelete (item); // recursively Delete subfolders
}
Directory. Delete (delFolderPath); // Delete an empty folder
}
/// <Summary>
///Folder copy
/// </Summary>
/// <Param name = "srcFolderPath"> </param>
/// <Param name = "destFolderPath"> </param>
Public static void FolderCopy (string srcFolderPath, string destFolderPath)
{
// Check whether the target directory ends with a target separator. If not, add it
If (destFolderPath [destFolderPath. Length-1]! = Path. DirectorySeparatorChar)
DestFolderPath + = Path. DirectorySeparatorChar;
// Determine whether the target directory exists. If not, create it
If (! Directory. Exists (destFolderPath ))
Directory. CreateDirectory (destFolderPath );
String [] fileList = Directory. GetFileSystemEntries (srcFolderPath );
Foreach (string file in fileList)
{
If (Directory. Exists (file ))
FolderCopy (file, destFolderPath + Path. GetFileName (file ));
Else
{
FileInfo fi = new FileInfo (file );
If (fi. Attributes. ToString (). IndexOf ("ReadOnly ")! =-1) // change the attribute of the read-only file. Otherwise, the file cannot be deleted.
Fi. Attributes = FileAttributes. Normal;
Try
{File. Copy (file, destFolderPath + Path. GetFileName (file), true );}
Catch (Exception e)
{
}
}
}
}
/// <Summary>
///Folder Movement
/// </Summary>
/// <Param name = "srcFolderPath"> </param>
/// <Param name = "destFolderPath"> </param>
Public static void FolderMove (string srcFolderPath, string destFolderPath)
{
// Check whether the target directory ends with a target separator. If not, add it
If (destFolderPath [destFolderPath. Length-1]! = Path. DirectorySeparatorChar)
DestFolderPath + = Path. DirectorySeparatorChar;
// Determine whether the target directory exists. If not, create it
If (! Directory. Exists (destFolderPath ))
Directory. CreateDirectory (destFolderPath );
String [] fileList = Directory. GetFileSystemEntries (srcFolderPath );
Foreach (string file in fileList)
{
If (Directory. Exists (file ))
{
FolderMove (file, destFolderPath + Path. GetFileName (file ));
// Directory. Delete (file );
}
Else
File. Move (file, destFolderPath + Path. GetFileName (file ));
}
Directory. Delete (srcFolderPath );
}
}