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);
}
}
{
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);
}
}