• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

【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/),如有转载请标明出处,不甚感激!!!

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#Ioc容器Unity,简单实用发布时间:2022-07-13
下一篇:
c#判断字符串是否为空或null发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap