[英]Fastest way to create and move backup files to a directory?
I want to create a backup file before performing any operation on the file and then move it a to a location with the folder structure being D:\\BACKUP\\(Date)\\(Time)
.我想在对文件执行任何操作之前创建一个备份文件,然后将其移动到文件夹结构为
D:\\BACKUP\\(Date)\\(Time)
。
What is the fastest way of doing it?最快的方法是什么? I'm currently doing it as below
我目前正在做如下
string path = textBox1.Text;
var files = Directory.GetDirectories(path, "dtpo", SearchOption.AllDirectories)
.SelectMany(t => Directory.GetFiles(t, "*.txt")).ToArray();
foreach (var file in files)
{
File.Copy(file,file+".bk",true);
string OnlyDate=DateTime.Today.ToString("dd-MM-yyyy");
string OnlyTime = DateTime.Now.ToString("hh-mm-ss");
if (!Directory.Exists(@"D:\BACKUP\" + OnlyDate+"\\"+OnlyTime))
{
Directory.CreateDirectory(@"D:\BACKUP\" + OnlyDate+"\\"+OnlyTime);
}
string rootFolderPath = Path.GetFullPath(file+".bk");
string targetPath=@"D:\BACKUP\" + OnlyDate+"\\"+OnlyTime+"\\"+Path.GetFileName(file+".bk");
File.Move(rootFolderPath, targetPath);
//Do the processes
}
Directory.exists can be bypassed, createdirectory won't run if it does. Directory.exists 可以被绕过,createdirectory 不会运行。 Also recommend you don't do 2 io operations to write the file.
还建议您不要进行 2 次 io 操作来写入文件。 Just write directly to the output using file.copy.
只需使用 file.copy 直接写入输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.