如何从一个路径复制文件夹并将其粘贴到使用c#的另一个路径? 复制的原始文件夹应当与今天的日期,如 “220310”如何从一个路径复制文件夹并将其粘贴到使用c#的另一个路径?
像命名的文件夹里粘贴.....
原单文件夹:C://组织/ AbcFolder
目标文件夹:d ://目的地/ 220310/AbcFolder
请帮助..
如何从一个路径复制文件夹并将其粘贴到使用c#的另一个路径? 复制的原始文件夹应当与今天的日期,如 “220310”如何从一个路径复制文件夹并将其粘贴到使用c#的另一个路径?
像命名的文件夹里粘贴.....
原单文件夹:C://组织/ AbcFolder
目标文件夹:d ://目的地/ 220310/AbcFolder
请帮助..
基本上你会使用
var files = Directory.GetFiles(originalFolder,"*.*",SearchOption.AllDirectories)
得到你想要复制所有文件,然后通过
Directory.Create(destinationFolder)
和环比原来的文件名创建目标目录(在文件),使用的FileInfo类来获得原始文件的路径,File.Copy()将文件复制到其新位置。 所有这些类都在System.IO命名空间中。
private static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting)
{
bool ret = true;
try
{
SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";
if (Directory.Exists(SourcePath))
{
if (Directory.Exists(DestinationPath) == false)
Directory.CreateDirectory(DestinationPath);
foreach (string fls in Directory.GetFiles(SourcePath))
{
FileInfo flinfo = new FileInfo(fls);
flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
}
foreach (string drs in Directory.GetDirectories(SourcePath))
{
DirectoryInfo drinfo = new DirectoryInfo(drs);
if (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false)
ret = false;
}
Directory.CreateDirectory(DI_Target + "//Database");
}
else
{
ret = false;
}
}
catch (Exception ex)
{
ret = false;
}
return ret;
}
你可以得到的日期时间这样的:
DateTime dtnow = DateTime.Now;
string strDate=dtnow.Day.ToString()+dtnow.Month.ToString()+dtnow.Year.ToString();
而对于副本的目录到另一个看到这个CodeProject上的文章:Function to copy a directory to another place (nothing fancy)
看起来这是一个额外的行'Directory.CreateDirectory( DI_Target +“//数据库”);' – vicentedealencar 2015-07-23 16:40:08