步骤:
1、遍历D盘Source文件夹找出所有名称包含LTE的文件,文件路径存放到List<string>中
2、遍历List<string>,把所有文件Copy到E盘的备份文件夹中
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该目录下面的文件
{