最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c#使用file.copy實(shí)現(xiàn)文件備份示例

 更新時(shí)間:2014年03月06日 12:02:11   作者:  
需要把D盤(pán)Source文件夾中的所有名稱(chēng)包含"LTE"的子文件夾Copy到E盤(pán)的Backup文件中,實(shí)現(xiàn)特定文件夾每天備份,下面使用file.copy實(shí)現(xiàn)一下這個(gè)功能

步驟:

1、遍歷D盤(pán)Source文件夾找出所有名稱(chēng)包含LTE的文件,文件路徑存放到List<string>中
2、遍歷List<string>,把所有文件Copy到E盤(pán)的備份文件夾中

復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.IO;

namespace CopyLTE
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string txtPath = @"..\..\Dir\directory.txt";
                string srcPath = "";
                string aimPath = "";
                string KeyWords = "";
                List<string> KeyFloderList = new List<string>();
                if (File.Exists(txtPath) == false)
                {
                    Console.WriteLine("directory.txt不存在,無(wú)法獲取Copy路徑");
                }
                else
                {
                    GetCopyPath(txtPath, out srcPath, out aimPath, out KeyWords);
                    if (srcPath == "" || aimPath == "" || KeyWords == "")
                    {
                        Console.WriteLine("請(qǐng)?jiān)赿irectory.txt,輸入Copy的源文件夾,目的文件,和KeyWords");
                    }
                    else
                    {
                        DirectoryInfo FolderSource = new DirectoryInfo(srcPath);
                        foreach (DirectoryInfo NextFolder in FolderSource.GetDirectories())
                        {
                            if (NextFolder.FullName.Contains(KeyWords))
                            {
                                KeyFloderList.Add(NextFolder.FullName);
                            }
                        }
                    }
                }
                if (KeyFloderList.Count > 0)
                {
                    foreach (string FloderPath in KeyFloderList)
                    {
                        //Copy From Source To Backup
                        CopyDirectory(FloderPath, aimPath);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //
        private static void CopyDirectory(string srcdir, string desdir)
        {
            string folderName = srcdir.Substring(srcdir.LastIndexOf("\\") + 1);

            string desfolderdir = desdir + "\\" + folderName;

            if (desdir.LastIndexOf("\\") == (desdir.Length - 1))
            {
                desfolderdir = desdir + folderName;
            }
            string[] filenames = Directory.GetFileSystemEntries(srcdir);

            foreach (string file in filenames)// 遍歷所有的文件和目錄
            {
                if (Directory.Exists(file))// 先當(dāng)作目錄處理如果存在這個(gè)目錄就遞歸Copy該目錄下面的文件
                {

                    string currentdir = desfolderdir + "\\" + file.Substring(file.LastIndexOf("\\") + 1);
                    if (!Directory.Exists(currentdir))
                    {
                        Directory.CreateDirectory(currentdir);
                    }

                    CopyDirectory(file, desfolderdir);
                }

                else // 否則直接copy文件
                {
                    string srcfileName = file.Substring(file.LastIndexOf("\\") + 1);

                    srcfileName = desfolderdir + "\\" + srcfileName;
                    if (File.Exists(srcfileName))
                    {
                        File.Delete(srcfileName); //Delete Old Files
                    }

                    if (!Directory.Exists(desfolderdir))
                    {
                        Directory.CreateDirectory(desfolderdir);
                    }
                    File.Copy(file, srcfileName);
                }
            }
        }
        //取得路徑
        public static void GetCopyPath(string strTxt, out string From, out string To, out string keyWords)
        {
            try
            {
                string[] stringLines = File.ReadAllLines(strTxt, Encoding.Default);
                if (stringLines.Length >= 3)
                {
                    From = stringLines[0].ToString();
                    To = stringLines[1].ToString();
                    keyWords = stringLines[2].ToString();
                }
                else
                {
                    From = "";
                    To = "";
                    keyWords = "";
                    Console.WriteLine("請(qǐng)?jiān)赿irectory.txt,輸入Copy的源文件夾,目的文件,和KeyWords");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

相關(guān)文章

  • C# using三種使用方法

    C# using三種使用方法

    這篇文章主要為大家詳細(xì)介紹了C# using三種使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • C#中Dictionary與List的用法區(qū)別以及聯(lián)系詳解

    C#中Dictionary與List的用法區(qū)別以及聯(lián)系詳解

    List和Dictionary想必是我們平常用到最多的C#容器了,他們使用起來(lái)都很簡(jiǎn)單,這篇文章主要給大家介紹了關(guān)于C#中Dictionary與List的用法區(qū)別以及聯(lián)系的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • C#實(shí)現(xiàn)跨線程操作控件方法

    C#實(shí)現(xiàn)跨線程操作控件方法

    這篇文章主要介紹了C#實(shí)現(xiàn)跨線程操作控件方法,主要采用異步訪問(wèn)方式實(shí)現(xiàn),需要的朋友可以參考下
    2014-10-10
  • 關(guān)于C#中ajax跨域訪問(wèn)問(wèn)題

    關(guān)于C#中ajax跨域訪問(wèn)問(wèn)題

    最近做項(xiàng)目,需要跨域請(qǐng)求訪問(wèn)數(shù)據(jù)問(wèn)題。下面通過(guò)本文給大家分享C#中ajax跨域訪問(wèn)代碼詳解,需要的朋友可以參考下
    2017-05-05
  • C#判斷多個(gè)文本框是否為空的方法

    C#判斷多個(gè)文本框是否為空的方法

    這篇文章主要介紹了C#判斷多個(gè)文本框是否為空的方法,可實(shí)現(xiàn)對(duì)多個(gè)文本框的遍歷、判斷及提示等功能,需要的朋友可以參考下
    2015-06-06
  • C# dump系統(tǒng)lsass內(nèi)存和sam注冊(cè)表詳細(xì)

    C# dump系統(tǒng)lsass內(nèi)存和sam注冊(cè)表詳細(xì)

    這篇文章主要介紹了C# dump系統(tǒng)lsass內(nèi)存和sam注冊(cè)表,在這里選擇 C# 的好處是體積小,結(jié)合 loadAssembly 方便免殺,希望對(duì)讀者們有所幫助
    2021-09-09
  • C#難點(diǎn)逐個(gè)擊破(2):out返回參數(shù)

    C#難點(diǎn)逐個(gè)擊破(2):out返回參數(shù)

    之前提到ref是將原方法中的參數(shù)影響的結(jié)果返回到調(diào)用它的方法中,out與ref類(lèi)似,相比之下,ref傳遞參數(shù)的地址,out是返回值。
    2010-02-02
  • C#中的虛函數(shù)virtual

    C#中的虛函數(shù)virtual

    這篇文章介紹了C#中的虛函數(shù)virtual,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • C#使用NPOI對(duì)Excel數(shù)據(jù)進(jìn)行導(dǎo)入導(dǎo)出

    C#使用NPOI對(duì)Excel數(shù)據(jù)進(jìn)行導(dǎo)入導(dǎo)出

    這篇文章介紹了C#使用NPOI對(duì)Excel數(shù)據(jù)進(jìn)行導(dǎo)入導(dǎo)出的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • C#實(shí)現(xiàn)飛行棋游戲

    C#實(shí)現(xiàn)飛行棋游戲

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)飛行棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07

最新評(píng)論

烟台市| 米林县| 屏山县| 儋州市| 新津县| 广饶县| 宜阳县| 翁牛特旗| 鹤山市| 南宫市| 庆安县| 锡林浩特市| 平和县| 洛扎县| 亳州市| 潮安县| 新绛县| 平定县| 恩施市| 彝良县| 鄂尔多斯市| 东平县| 洞口县| 洪泽县| 樟树市| 恩平市| 明光市| 宁化县| 长沙县| 和龙市| 阜阳市| 库伦旗| 沁源县| 麻江县| 波密县| 广昌县| 扶沟县| 高安市| 崇文区| 乐东| 离岛区|