Replication method of the directory in .NET

xiaoxiao2021-03-17  217

A directory replication method used to copy all the contents of a directory to another directory. Just create and copy all files in the current folder with a recursive backtracking method.

Private void DirectoryCopy (String SourceDirectory) {if (! Directory.exists (SourceDirectory) ||! Directory.exists (targetdirectory) {Return;}

DirectoryInfo SourceInfo = New DirectoryInfo (SOURCEDIRECTORY); fileInfo [] fileInfo = sourceInfo.getfiles ();

Foreach (fileInfo fitemp in fileinfo) {file.copy (SOURCEDIRECTORY "//" fitemp.name, TargetDirectory "//" fitemp.name, true);}

DirectoryInfo [] diinfo = sourceInfo.getdirectories ();

foreach (DirectoryInfo diTemp in diInfo) {string sourcePath = diTemp.FullName; string targetPath = diTemp.FullName.Replace (sourceDirectory, targetDirectory); Directory.CreateDirectory (targetPath); directoryCopy (sourcePath, targetPath);}}

转载请注明原文地址:https://www.9cbs.com/read-129667.html

New Post(0)