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;
}
}
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
为Android应用增加渠道信息 自动化不同渠道的打包过程的使用详解
springboot整合JSR303校验功能实现代码
![](https://img.aspxhome.com/file/2023/8/72378_0s.jpg)
HelloSpringMVC注解版实现步骤解析
浅谈Java内省机制
![](https://img.aspxhome.com/file/2023/2/106152_0s.png)
Spring核心IoC容器的依赖注入接口和层级包命名规范
![](https://img.aspxhome.com/file/2023/6/109946_0s.jpg)
Spring Cloud集成Nacos Config动态刷新源码剖析
![](https://img.aspxhome.com/file/2023/7/83557_0s.png)
C#绘制中国象棋棋盘
![](https://img.aspxhome.com/file/2023/1/94551_0s.jpg)
Java使用TCP套接字实现多人聊天功能详解
C#装箱和拆箱操作实例分析
javascript 在线文本编辑器实现代码
使用Java实现类似Comet风格的web app
Java8方法引用及构造方法引用原理实例解析
Java利用future及时获取多线程运行结果
Android使用Gradle依赖配置compile、implementation与api的区别介绍
Java八种基本变量作为类的成员变量的默认值操作
Java 提取照片的EXIF信息批量重命名
![](https://img.aspxhome.com/file/2023/8/80838_0s.png)
Java 类与对象超基础讲解
![](https://img.aspxhome.com/file/2023/8/70938_0s.png)
Java实现在Word指定位置插入分页符
![](https://img.aspxhome.com/file/2023/1/75451_0s.png)
基于WPF实现验证码控件
![](https://img.aspxhome.com/file/2023/5/92975_0s.png)