c#之IO基础《复制文件夹》

/*
 * 由SharpDevelop创建。
 * 用户: hubiao
 * 日期: 2014/3/2
 * 时间: 20:32
 * 
 * <span style="white-space:pre">	</span>本工具为测序程序!!!!!
 */
using System;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Text.RegularExpressions;
	
namespace demo_6
{
	class Program
	{
		private static UInt64 fileCount = 0;
		private static UInt64 dirCount = 0;
		
		
		public static void Main(string[] args)
		{
		/**
		 * 	需求:复制文件夹
		 * 		思路:
* 					1:获取所有文件目录。
* 						如果是文件对象,则开始复制
						如果是文件夹,则在目标之间创建文件夹,再递归继续!
					2:对文件夹路径,采用先替换,再通过Directory来创建。
							如:c:\\abc\aaa
								替换成d:\\img\\aaa  再判断这个是否存在,不存在则创建。
				
				本代码有个bug:目前还没把保存路径 做校验判断。希望大家指点....
					^(([a-zA-Z]:\\)|(\\{2}\w+)\$?)((([^/\\\?\*])(\\?))*)$可以用这个,但出错...
		 */
			//string theSourceFile = "D:\\imggg";
			//string tager = "e:\\imgMy";
			
			//记录日志
			string log = "c:\\copyFileError.txt";
			StreamWriter sw =  new StreamWriter(log,true);  
			DateTime  date = System.DateTime.Now;
			
			while(true){
				sw.WriteLine("\r\n"+date.ToLocalTime());
				
				//开始计算时间
				Stopwatch watch = new Stopwatch();
				System.Console.Write("\r\n-----------复制小工具(结束系统输入:x)----------\r\n");
				System.Console.Write("请输入源文件路径:");
				
				string theSourceFile = Console.ReadLine();
				if("X".Equals(theSourceFile.ToUpper())){
				   	break;
				}
				
				System.Console.Write("\r\n请输入保存路径:");
				string tager = Console.ReadLine();
				
				if("X".Equals(tager.ToUpper())){
					break;
				}
				
				//处理掉:\\ 
				theSourceFile = theSourceFile.Replace("\\\\","\\");
				tager = tager.Replace("\\\\","\\");
				watch.Start();
					try{
						DirectoryInfo theSourceFileInfo = new DirectoryInfo(theSourceFile);
						FileSystemInfo[] systemInfo = theSourceFileInfo.GetFileSystemInfos();
						traversal(systemInfo,theSourceFile,tager);
					}catch(Exception e){
						Console.WriteLine("\r\n找不到路径,请重新输入\r\n");
						sw.WriteLine(e);
					}
				watch.Stop();
				string timeStr = watch.Elapsed.Seconds.ToString();
				int  timeNum = int.Parse(timeStr);
				Console.WriteLine("\r\n本次复制文件{0}个、文件夹{1}个,耗时{2}秒。",fileCount,dirCount,(timeNum>0)?timeNum+1:timeNum);
				sw.WriteLine("本次复制文件{0}个、文件夹{1}个,耗时{2}秒。",fileCount,dirCount,(timeNum>0)?timeNum+1:timeNum);
				fileCount = 0;
				dirCount = 0;
				sw.Flush();
			}
			sw.Close();
			Console.WriteLine("\r\n\r\n系统已结束,请输入任意键关闭窗口.....^_^!");
			Console.ReadKey(true);
		
		}
		public static  bool IsPositiveNumber(String path) {
		    Regex objNotPositivePattern = new Regex(@"^([a-zA-Z]:\\)?[^\/\:\*\?\""\<\>\|\,]+$");
		    return objNotPositivePattern.IsMatch(path);
		}
		private static void traversal(FileSystemInfo[] info,string theSourceFile,string tager){
			foreach(FileSystemInfo fsi in info){
				FileInfo fileInfo = fsi as FileInfo;
				if(fileInfo!=null){
					fileCount++;
					string fileName = fileInfo.FullName;
					fileName = fileName.Replace(theSourceFile,tager);
					System.Console.WriteLine("正在复制{0}:",fileName);
					File.Copy(fileInfo.FullName,fileName);
				}
				DirectoryInfo  dirInfo = fsi as DirectoryInfo;
				if(dirInfo!=null){
					string dirName= dirInfo.FullName;
					dirName = dirName.Replace(theSourceFile,tager);
					//如果不存在,则新创建
					if(!File.Exists(dirName)){
						dirCount++;
						Directory.CreateDirectory(dirName);
					}
					System.Console.WriteLine("正在创建{0}",dirName);
					traversal(dirInfo.GetFileSystemInfos(),theSourceFile,tager);
				}
			}
		}
	}
}


 
 
 
 
 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用C#的System.IO类来复制文件夹到服务器。以下是示例代码: ```csharp using System.IO; string sourceDir = @"C:\SourceFolder"; string destinationDir = @"\\ServerName\DestinationFolder"; // Create the destination directory if it does not exist if (!Directory.Exists(destinationDir)) { Directory.CreateDirectory(destinationDir); } // Get the files from the source directory and copy them to the destination directory foreach (string file in Directory.GetFiles(sourceDir)) { string destFile = Path.Combine(destinationDir, Path.GetFileName(file)); File.Copy(file, destFile, true); } // Get the subdirectories from the source directory and copy them to the destination directory foreach (string subDir in Directory.GetDirectories(sourceDir)) { string destSubDir = Path.Combine(destinationDir, Path.GetFileName(subDir)); Directory.CreateDirectory(destSubDir); CopyDirectory(subDir, destSubDir); } ``` 在示例代码,`sourceDir`变量表示源文件夹的路径,`destinationDir`变量表示目标服务器上的文件夹路径。首先,我们检查目标文件夹是否存在,如果不存在,则创建一个。然后,我们使用`Directory.GetFiles`方法获取源文件夹的所有文件,并使用`File.Copy`方法将它们复制到目标文件夹。接下来,我们使用`Directory.GetDirectories`方法获取源文件夹的所有子文件夹,并使用`Directory.CreateDirectory`方法创建它们在目标文件夹的对应项。最后,我们递归地调用`CopyDirectory`方法以处理每个子文件夹的文件和子文件夹。 注意,这个示例代码没有包括错误处理和异常处理,你可能需要根据你的具体情况添加它们。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值