欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

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

时间:2020-10-14 10:06:17|栏目:.NET代码|点击:

步骤:

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不存在,?o法?@取Copy路??");
                }
                else
                {
                    GetCopyPath(txtPath, out srcPath, out aimPath, out KeyWords);
                    if (srcPath == "" || aimPath == "" || KeyWords == "")
                    {
                        Console.WriteLine("?在directory.txt,?入Copy的源文件?A,目的文件,和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的源文件?A,目的文件,和KeyWords");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

上一篇:C# 实现抓取网站页面内容的实例方法

栏    目:.NET代码

下一篇:使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)

本文标题:c#使用file.copy实现文件备份示例

本文地址:http://www.codeinn.net/misctech/11243.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有