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
投稿

猜你喜欢

  • 带你详细了解Java值传递和引用传递

    2023-02-19 08:42:26
  • Android仿QQ在状态栏显示登录状态效果

    2021-10-09 04:12:06
  • SpringCloud Eureka实现服务注册与发现

    2021-10-25 11:41:58
  • OpenGL Shader实现光照发光体特效

    2022-03-16 18:51:50
  • 通过spring boot 设置tomcat解决 post参数限制问题

    2022-09-26 23:38:31
  • Lombok为啥这么牛逼?SpringBoot和IDEA官方都要支持它

    2021-10-18 23:04:50
  • ava实现一致性Hash算法

    2022-09-18 00:44:33
  • ASM的tree api对匿名线程的hook操作详解

    2022-07-16 15:03:30
  • C# zxing二维码写入的实例代码

    2021-09-01 12:23:26
  • Android开发实现的标准体重计算器功能示例

    2023-01-19 02:56:48
  • C#通过重写Panel改变边框颜色与宽度的方法

    2021-07-09 05:57:52
  • Android中Fragment的基本用法示例总结

    2021-11-25 19:00:23
  • unity 如何使用LineRenderer 动态划线

    2021-10-27 03:42:50
  • C#实现过滤html标签并保留a标签的方法

    2023-02-06 17:30:24
  • Android实现简易计算器(可以实现连续计算)

    2023-11-30 23:29:47
  • Android稳定性:可远程配置化的Looper兜底框架

    2022-02-18 09:02:23
  • spring boot 即时重新启动(热更替)使用说明

    2023-01-19 02:41:05
  • clion最新激活码+汉化的步骤详解(亲测可用激活到2089)

    2023-07-17 08:03:53
  • Kotlin实现在类里面创建main函数

    2022-08-26 20:11:13
  • Java Idea TranslationPlugin翻译插件使用解析

    2023-12-01 10:25:02
  • asp之家 软件编程 m.aspxhome.com