簡體   English   中英

創建備份文件並將其移動到目錄的最快方法?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM