. NET instance method for creating, deleting, and copying folders and Their subfiles

Source: Internet
Author: User

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

}

}

}

}

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.