代码示例:递归遍历文件夹实现目录拷贝

代码很简单。因为有人在坛子里问,所以从好久以前的旧工程里面搜出了这段代码。


		public static bool CopyDirectory(string pathSrc, string pathDst)
		{
			if(!Directory.Exists(pathSrc))
			{
				return false;
			}
			
			CreateFullPath(pathDst);
			
			DirectoryInfo directorySrc = new DirectoryInfo(pathSrc);
			DirectoryInfo directoryDst = new DirectoryInfo(pathDst);
			
			CopyDirectory(directorySrc, directoryDst);
			return true;
		}
		
		private static void CopyDirectory(DirectoryInfo srcDictionary, DirectoryInfo dstDictionary)
		{
			FileInfo[] srcFiles = srcDictionary.GetFiles();
			foreach(FileInfo srcFile in srcFiles)
			{
				File.Copy(srcFile.FullName, Path.Combine(dstDictionary.FullName, srcFile.Name), true);
			}
			
			DirectoryInfo[] directorySrcArray = srcDictionary.GetDirectories();
			foreach(DirectoryInfo directorySrc in directorySrcArray)
			{
				string dstDirectoryFullPath = Path.Combine(dstDictionary.FullName, directorySrc.Name);
				DirectoryInfo directoryDst = new DirectoryInfo(dstDirectoryFullPath);
				
				CreateFullPath(directoryDst.FullName);
				
				CopyDirectory(directorySrc, directoryDst);
			}
		}


你可能感兴趣的:(String)