Copy folder for folder action classes in C # encapsulation
I. Copy folder principle:
1. Recursively traverse a folder
2. Copying files
Two. FolderHelper.cs
1 /// <summary>2 ///Folder Action Class3 /// </summary>4 Public Static classFolderhelper5 {6 /// <summary>7 ///Copy Folder8 /// </summary>9 /// <param name= "Sourcefoldername" >source folder Directory</param>Ten /// <param name= "Destfoldername" >Destination Folder Directory</param> One Public Static voidCopy (stringSourcefoldername,stringdestfoldername) A { -Copy (Sourcefoldername, Destfoldername,false); - } the - /// <summary> - ///Copy Folder - /// </summary> + /// <param name= "Sourcefoldername" >source folder Directory</param> - /// <param name= "Destfoldername" >Destination Folder Directory</param> + /// <param name= "Overwrite" >allow overwriting of files</param> A Public Static voidCopy (stringSourcefoldername,stringDestfoldername,BOOLoverwrite) at { - varSourcefilespath =directory.getfilesystementries (sourcefoldername); - - for(inti =0; i < sourcefilespath.length; i++) - { - varSourcefilepath =Sourcefilespath[i]; in varDirectoryName =Path.getdirectoryname (sourcefilepath); - varForlders = Directoryname.split ('\\'); to varLastdirectory = Forlders[forlders. Length-1]; + varDest =Path.Combine (Destfoldername, lastdirectory); - the if(File.exists (sourcefilepath)) * { $ varsourceFileName =Path.getfilename (sourcefilepath);Panax Notoginseng if(!directory.exists (dest)) - { the directory.createdirectory (dest); + } A file.copy (Sourcefilepath, Path.Combine (dest, sourceFileName), overwrite); the } + Else - { $ Copy (Sourcefilepath, dest, overwrite); $ } - } - } the}
Copy folder for folder action classes in C # encapsulation