码迷,mamicode.com
首页 > Windows程序 > 详细

C# 根据时间获取目录及文件

时间:2015-03-16 19:12:20      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

实际开发中遇到的问题,记录下来以便以后查看

业务:每天自动生成一个目录,在按日期生成的目录中会生成N个子目录,

         子目录中会生成一至两个文件,先在需要取到子目录中的文件 

  1 using System;
  2 using System.IO;
  3 using System.Collections.Generic;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 
  8 namespace MULUTEST
  9 {
 10     class Program
 11     {
 12         //返回文件列表 
 13         private static List<String>  filelist;
 14         private static int fileDirJB=2;
 15 
 16         static void Main(string[] args)
 17         {
 18             string dir = "E:\\mulutest";
 19             Console.WriteLine("当前目录:" + dir);
 20             try
 21             {
 22                 List<String> datelist = getDateDir();
 23                 if (datelist != null && datelist.Count > 0) 
 24                 {   filelist=new List<string>();
 25                     foreach (string datemulu in datelist)
 26                     {
 27                         Console.WriteLine("检查日期" + datemulu + "的目录");
 28                         string mulu = dir + "\\" + datemulu;                        
 29                         ListFiles(new DirectoryInfo(mulu),"",1);
 30                     }                  
 31                 }
 32 
 33                 Console.WriteLine("最终文件:");
 34                 foreach (string filename in filelist) {
 35                     Console.WriteLine(filename);
 36                 }
 37                 Console.ReadLine();
 38             }
 39             catch (IOException e)
 40             {
 41                 Console.WriteLine(e.Message);
 42             } 
 43         }
 44 
 45         #region 按时间 获取文件夹名称
 46         public static List<String> getDateDir() 
 47         {
 48             DateTime testdaay = Convert.ToDateTime("2015-03-11");
 49             Console.WriteLine("最后生成时间:" + testdaay.ToString("yyyyMMdd"));
 50             DateTime dt = DateTime.Today;
 51             Console.WriteLine("今天:" + dt.ToString("yyyyMMdd"));
 52             List<String> datelist = new List<string>();
 53 
 54             while (DateTime.Compare(dt, testdaay) > 0)
 55             {
 56                 testdaay = testdaay.AddDays(1);
 57                 datelist.Add(testdaay.ToString("yyyyMMdd"));
 58             }
 59             return datelist;
 60         }
 61         #endregion
 62 
 63         public static void ListFiles(FileSystemInfo info, string fjinfo,int mulujb)
 64         {
 65             if (fjinfo != null && !fjinfo.Equals(""))
 66             {
 67                 Console.WriteLine("开始查找目录:" + fjinfo + "\\" + info.ToString());
 68             }
 69             else {
 70                 Console.WriteLine("开始查找目录:"  + info.ToString());
 71             }
 72 
 73             if (!info.Exists) {
 74                 Console.WriteLine("不存在目录:"+info.ToString());
 75                 return;
 76             }
 77 
 78             DirectoryInfo dir = info as DirectoryInfo;
 79             //不是目录 
 80             if (dir == null) {
 81                 Console.WriteLine("不是目录: "+info.ToString());
 82                 return;
 83             } 
 84 
 85             FileSystemInfo[] files = dir.GetFileSystemInfos();
 86             for (int i = 0; i < files.Length; i++)
 87             {
 88                 FileInfo file = files[i] as FileInfo;
 89                 //是文件 
 90                 if (file != null)
 91                 {
 92                     if (mulujb == fileDirJB)
 93                     {
 94                         filelist.Add(file.FullName);
 95                         Console.WriteLine("找到合法文件:" + file.FullName);
 96                     }
 97                     else {
 98                         Console.WriteLine("找到文件:" + file.FullName+" 不合法 ,忽略");
 99                     }                 
100                 }
101                 //对于子目录,进行递归调用 
102                 else
103                 {
104                     int jib = mulujb + 1;
105                     ListFiles(files[i], info.ToString(),jib);
106                 }
107 
108             }
109         } 
110     }
111 }

 

C# 根据时间获取目录及文件

标签:

原文地址:http://www.cnblogs.com/lxiaod/p/4342427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!