C # copy, move, and delete files and folders

Source: Internet
Author: User

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 );
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.