c#使用file.copy实现文件备份示例

时间:2021-06-03 05:13:13 

步骤:

1、遍历D盘Source文件夹找出所有名称包含LTE的文件,文件路径存放到List<string>中
2、遍历List<string>,把所有文件Copy到E盘的备份文件夹中


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不存在,無法獲取Copy路徑");
                }
                else
                {
                    GetCopyPath(txtPath, out srcPath, out aimPath, out KeyWords);
                    if (srcPath == "" || aimPath == "" || KeyWords == "")
                    {
                        Console.WriteLine("請在directory.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))// 先当作目录处理如果存在这个目录就递归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("請在directory.txt,輸入Copy的源文件夾,目的文件,和KeyWords");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

标签:file.copy,文件备份
0
投稿

猜你喜欢

  • 为Android应用增加渠道信息 自动化不同渠道的打包过程的使用详解

    2022-06-01 11:38:22
  • springboot整合JSR303校验功能实现代码

    2023-10-11 21:46:25
  • HelloSpringMVC注解版实现步骤解析

    2021-07-21 01:58:42
  • 浅谈Java内省机制

    2021-06-13 17:54:05
  • Spring核心IoC容器的依赖注入接口和层级包命名规范

    2021-12-31 02:22:57
  • Spring Cloud集成Nacos Config动态刷新源码剖析

    2022-04-16 11:35:13
  • C#绘制中国象棋棋盘

    2021-05-27 15:08:44
  • Java使用TCP套接字实现多人聊天功能详解

    2023-12-16 15:42:40
  • C#装箱和拆箱操作实例分析

    2021-10-25 13:17:36
  • javascript 在线文本编辑器实现代码

    2023-11-24 23:07:24
  • 使用Java实现类似Comet风格的web app

    2023-04-01 10:23:22
  • Java8方法引用及构造方法引用原理实例解析

    2022-07-24 08:21:22
  • Java利用future及时获取多线程运行结果

    2022-02-18 21:57:30
  • Android使用Gradle依赖配置compile、implementation与api的区别介绍

    2023-09-27 18:11:40
  • Java八种基本变量作为类的成员变量的默认值操作

    2022-06-25 04:55:58
  • Java 提取照片的EXIF信息批量重命名

    2023-10-05 14:11:28
  • Java 类与对象超基础讲解

    2023-06-12 00:03:22
  • Java实现在Word指定位置插入分页符

    2021-06-29 03:24:44
  • 基于WPF实现验证码控件

    2021-08-15 21:44:36
  • java 实现通过 post 方式提交json参数操作

    2022-08-29 05:00:16
  • asp之家 软件编程 m.aspxhome.com