[英]Fastest way to create and move backup files to a directory?
我想在對文件執行任何操作之前創建一個備份文件,然后將其移動到文件夾結構為D:\\BACKUP\\(Date)\\(Time)
。
最快的方法是什么? 我目前正在做如下
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 可以被繞過,createdirectory 不會運行。 還建議您不要進行 2 次 io 操作來寫入文件。 只需使用 file.copy 直接寫入輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.