如何将文件从父源文件夹移动到C#中的目标文件夹? [英] How to move files from a parent source folder to a destination folder in C#?

查看:134
本文介绍了如何将文件从父源文件夹移动到C#中的目标文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文件从父源文件夹移动到c#中的目标文件夹。在父源文件夹中,将有一个父子文件夹,并且在其中有多个子文件夹,如果子文件夹中存在任何文件,则这些文件必须以与父源文件夹相同的结构进行存档。



我试图调用递归方法,但归档仅适用于单个父子文件夹。



我有什么试过:



I want to move files from a Parent source folder to a destination folder in c#. In the parent source folder there would be a parent sub folder and with in it multiple sub folders and if any file exists in the sub folders those files must get archived with same structure as that of the parent source folder.

I tried to call a recursive method but archiving works for only a single parent sub folder.

What I have tried:

public void TEST(string source,string dest)
   {

       string tempsource = string.Empty;
       string tempdest = string.Empty;

       if (string.IsNullOrEmpty(dest)==false)
       {

           DirectoryInfo dirInfo = new DirectoryInfo(dest);
           if (dirInfo.Exists == false)
               Directory.CreateDirectory(dest);


           DirectoryInfo dir = new DirectoryInfo(source);
           DirectoryInfo[] dirs = dir.GetDirectories();

           int archivalagefield = 0;


           foreach (DirectoryInfo subdir in dirs)
           {
               tempsource = Path.Combine(source, subdir.Name);
               tempdest = Path.Combine(dest, subdir.Name);
               if (!Directory.Exists(tempdest))
                   try
                   {
                       //Directory.Move(subdir.FullName, temppath);
                       Directory.CreateDirectory(tempdest);
                       string[] files = Directory.GetFiles(tempsource);
                       GetArchiveLogDate GA = new GetArchiveLogDate();
                        DataSet dsGetArchiveAgeDate = GA.FetchMaxArchiveDateValue();
                        string ArchivalAge = dsGetArchiveAgeDate.Tables[1].Rows[0]["PARAMVALUE"].ToString().Trim();
                        if (dsGetArchiveAgeDate.Tables.Count > 0)
                        {
                            if (dsGetArchiveAgeDate.Tables[1].Rows.Count > 0)
                            {
                                archivalagefield = int.Parse(ArchivalAge) * -1;
                            }
                        }

                       foreach (string file in files)
                       {
                           try
                           {
                               DateTime FromDateTime = DateTime.Now.AddDays(ArchivalAgeField);
                               DateTime FromDate = Convert.ToDateTime(FromDateTime.ToString("yyyy/MM/dd"));
                               DateTime dtFileCreatedDate = File.GetCreationTime(file);


                               if (dtFileCreatedDate <= FromDate) // Only if the CreatedDate is less than FromDate, directory needs to be created and file needs to be moved
                               {
                                   string name = Path.GetFileName(file);
                                   string destFile = Path.Combine(tempdest, name);

                                   if (name != "file") File.Move(file, destFile);
                               }
                           }
                           catch
                           {

                           }
                       }

                   }
                   catch
                   {

                   }
           }
       }
       tempsource = Path.Combine(tempsource, appendedfolder);
       tempdest = Path.Combine(tempdest, appendedfolder);
       TEST(tempsource, tempdest);
   }

推荐答案

您需要一个纯C#解决方案吗?因为大多数人都会使用像rsync(POSIX)或robocopy(Windows)这样的实用程序,因为它内置了许多镜像/复制功能。只需复制存档中的结构并移动文件即可。如果这个练习是学术性的,我就是重新发明轮子,但在现实世界中,你使用一个经过验证的工具来完成它的任务。
Do you need a pure C# solution for this? Because most folks would just use a utility like rsync (POSIX) or robocopy (Windows) since a lot of the mirroring/replication functionality is built into it. Just replicate the structure in your archive and move the files. I'm all for reinventing the wheel if this exercise is academic, but in the real world, you use a proven tool to perform its given task.


这篇关于如何将文件从父源文件夹移动到C#中的目标文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆