.NET實現(xiàn)簡易的文件增量備份程序
來源:易賢網(wǎng) 閱讀:811 次 日期:2016-08-06 13:43:26
溫馨提示:易賢網(wǎng)小編為您整理了“.NET實現(xiàn)簡易的文件增量備份程序”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了.NET實現(xiàn)簡易的文件增量備份程序,需要的朋友可以參考下

.Net中提供了許多方便使用的方法,包括在處理文件中查找文件、拷貝文件等,今天實現(xiàn)的是通過簡易的程序?qū)崿F(xiàn)增量的備份文件。

首先需要的是選擇備份源文件路徑SourcePath和備份目標(biāo)文件路徑DestinationPath,然后通過StopWatch統(tǒng)計拷貝所耗費的時間。(注意:使用StopWatch需要添加 using System.Diagnostics命名空間,對文件的讀寫需要添加 using System.IO命名空間)。

/// <summary>

/// 增量備份函數(shù)方法

/// </summary>

/// <param name="SourcePath">備份源文件路徑</param>

/// <param name="DestinationPath">備份目標(biāo)文件路徑</param>

public void CopyDirectory(String SourcePath, String DestinationPath){

  Stopwatch watch = new Stopwatch();

  watch.Start();   //開始計算時間

  // 檢查目標(biāo)目錄是否以目錄分割字符結(jié)束如果不是則添加

  if (DestinationPath[DestinationPath.Length - 1] != Path.DirectorySeparatorChar)

  {

   DestinationPath += Path.DirectorySeparatorChar;

  }

  //判斷目標(biāo)目錄是否存在如果不存在則新建

  if (!Directory.Exists( DestinationPath))

  {

   Directory.CreateDirectory(DestinationPath);

  }

  // 得到源目錄的文件列表,該里面是包含文件以及目錄路徑的一個數(shù)組

  string[] fileList = Directory.GetFileSystemEntries(SourcePath);

  // 遍歷所有的文件和目錄

  foreach (string SourceFilename in fileList)

   {

    string filename = Path.GetFileName(SourceFilename);

     //先判斷文件在目標(biāo)文件夾中是否存在

     if (File.Exists(DestinationPath + filename))

      {

       FileInfo oldFile = new FileInfo(SourceFilename);

       FileInfo newFile = new FileInfo(DestinationPath + filename);

       if (oldFile.LastWriteTime == newFile.LastWriteTime) 

        {

          continue;     //跳出本次循環(huán)

        }

       }      else {

       // 先當(dāng)作目錄處理如果存在這個目錄就遞歸Copy該目錄下面的文件

       if (Directory.Exists(SourceFilename))

        {

          CopyDirectory(SourceFilename, DestinationPath + filename);

        }// 否則直接Copy文件

        else {

          File.Copy(SourceFilename, DestinationPath + filename, true);          }

       }

   }

  watch.Stop();  //時間停止  MessageBox.Show("備份完成 耗時"+watch.Elapsed+""); //顯示所消耗的時間

}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:.NET實現(xiàn)簡易的文件增量備份程序
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報警專用圖標(biāo)