Copy指定目录下的所有文件到新位置

public void CreateAllDirOrFiles(string oldPath, string newPath)
 {
  string[] strfiles = Directory.GetFiles(oldPath);
  foreach (string f in strfiles)
  {
   string strFullPath = newPath + "\\" + Path.GetFileName(f);
   if (!Directory.Exists(newPath))
   {
    Directory.CreateDirectory(newPath);
   }
   try
   {
    File.Copy(f, strFullPath,true);
   }
   catch (Exception exp)
   {
    throw exp;
   }
  }
  string[] strDirs = Directory.GetDirectories(oldPath);
  foreach (string dir in strDirs)
  {
   int pos = dir.LastIndexOf("\\");
   string strDirName = dir.Substring(pos + 1, dir.Length - pos - 1);
   string strTempOldPath = oldPath + "\\" + strDirName;
   string strTempNewPath = newPath + "\\" + strDirName;
   CreateAllDirOrFiles(strTempOldPath, strTempNewPath);
  }
 }
posted @ 2010-07-27 21:18  南阳·源  阅读(184)  评论(0编辑  收藏  举报