C#实现根据传入时间段,找出时间段内日期,并生成相对应文件路径

【1】获取固定日期范围内的所有日期,以数组形式返回

/// <summary>
        /// 获取固定日期范围内的所有日期,以数组形式返回
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        private DateTime[] GetAllDays(DateTime startTime ,DateTime endTime) {
            List<DateTime> listDay = new List<DateTime>();
            DateTime dtDay = new DateTime();
            //循环比较,取出日期;
            for (dtDay = startTime; dtDay.CompareTo(endTime) <= 0;dtDay = dtDay.AddDays(1))
            {
                listDay.Add(dtDay);
            }
            return listDay.ToArray();
        }

 

我们来检测一下,在Main函数中编写如下代码:

static void Main(string[] args)
        {
            System.DateTime currentTime = System.DateTime.Now;
            DateTime startTime = currentTime.AddDays(-11);
            DateTime endTime = currentTime.AddDays(-1);
            //Console.WriteLine(currentTime);
            Program getDate = new Program();
            DateTime[] dateList = getDate.GetAllDays(startTime, endTime);
            for (int i = 0; i < dateList.Length; i++)
            {
                Console.WriteLine(dateList[i]);
            }
        }

运行结果如下:

2017/6/18 14:58:18
2017/6/19 14:58:18
2017/6/20 14:58:18
2017/6/21 14:58:18
2017/6/22 14:58:18
2017/6/23 14:58:18
2017/6/24 14:58:18
2017/6/25 14:58:18
2017/6/26 14:58:18
2017/6/27 14:58:18
2017/6/28 14:58:18

 

【3】我们顺便再拓展一点,根据上面的日期再生成路径

  /// <summary>
        /// 根据传入时间段生成路径
        /// </summary>
        /// <param name="filePath"></param>
        public List<String> GeneratePath(DateTime startTime,DateTime endTime) {
            string FileName;
            List<String> filePathList = new List<string>();
            string CurDir = System.AppDomain.CurrentDomain.BaseDirectory + @"SaveDir";
            String filePath = "";
            DateTime[] SubTimeList = GetAllDays(startTime,endTime);
            for (int i = 0; i < SubTimeList.Length;i++ )
            {
                string subStr = SubTimeList[i].ToString("yyyyMMdd");
                FileName = "MyFileSend" + subStr + ".txt";
                filePath = CurDir + FileName;
                filePathList.Add(filePath);
            }
            return filePathList;       
        }

我们来检测一下,在Main函数中编写如下代码:

/// <summary>
        /// 获取文件中的数据
        /// </summary>
        /// <param name="filePath"></param>
        public static string fileToString(String filePath){
            string strData = "";
            try
            {
                string line;
                // 创建一个 StreamReader 的实例来读取文件 ,using 语句也能关闭 StreamReader
                using (System.IO.StreamReader sr = new System.IO.StreamReader(filePath))
                {                  
                    // 从文件读取并显示行,直到文件的末尾
                    while ((line = sr.ReadLine()) != null)
                    {
                        //Console.WriteLine(line);
                        strData = line;
                    }                    
                }              
            }
            catch (Exception e)
            {
                // 向用户显示出错消息
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
            return strData;
        }

        static void Main(string[] args)
        {
            System.DateTime currentTime = System.DateTime.Now;
            DateTime startTime = currentTime.AddDays(-11);
            DateTime endTime = currentTime.AddDays(-1);
            //Console.WriteLine(currentTime);
            Program getDate = new Program();
            DateTime[] dateList = getDate.GetAllDays(startTime, endTime);
            List<String> filePathList = new List<string>();
            filePathList = getDate.GeneratePath(startTime, endTime);
            Console.WriteLine(filePathList);
            for (int i = 0; i < filePathList.Count; i++ )
            {
                Console.WriteLine(filePathList[i]);
            }
        }

运行结果如下:

D:\VSProject\SavaProcessToFile\SavaProcessToFile\bin\Debug\SaveDirMyFileSend20170618.txt
D:\VSProject\SavaProcessToFile\SavaProcessToFile\bin\Debug\SaveDirMyFileSend20170619.txt
D:\VSProject\SavaProcessToFile\SavaProcessToFile\bin\Debug\SaveDirMyFileSend20170620.txt
D:\VSProject\SavaProcessToFile\SavaProcessToFile\bin\Debug\SaveDirMyFileSend20170621.txt
D:\VSProject\SavaProcessToFile\SavaProcessToFile\bin\Debug\SaveDirMyFileSend20170622.txt
D:\VSProject\SavaProcessToFile\SavaProcessToFile\bin\Debug\SaveDirMyFileSend20170623.txt
D:\VSProject\SavaProcessToFile\SavaProcessToFile\bin\Debug\SaveDirMyFileSend20170624.txt
D:\VSProject\SavaProcessToFile\SavaProcessToFile\bin\Debug\SaveDirMyFileSend20170625.txt
D:\VSProject\SavaProcessToFile\SavaProcessToFile\bin\Debug\SaveDirMyFileSend20170626.txt
D:\VSProject\SavaProcessToFile\SavaProcessToFile\bin\Debug\SaveDirMyFileSend20170627.txt
D:\VSProject\SavaProcessToFile\SavaProcessToFile\bin\Debug\SaveDirMyFileSend20170628.txt

好了,不早了,今天就讲到这儿了;

 

本文源于zhuxiaoge(http://www.cnblogs.com/zhuxiaoge/p/7094513.html),如有转载请标明出处,不甚感激!!!

 

posted @ 2017-06-29 15:07  朱小哥  阅读(3970)  评论(0编辑  收藏  举报