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

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

  1. Matlab.NET混合编程技巧之——找出Matlab内置函数

    原文:[原创]Matlab.NET混合编程技巧之--找出Matlab内置函数 Matlab与.NET的混合编程,掌握了基本过程,加上一定的开发经验和算法基础,肯定不难.反之,有时候一个小错误,可能抓破 ...

  2. 【原创】Matlab.NET混合编程技巧之找出Matlab内置函数

                  本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新    Matlab和C#混合编程文章目录 :[目录]Matlab和C#混合编程文章目录 Matlab与.N ...

  3. Matlab.NET混编技巧之——找出Matlab内置函数

    原文 http://www.cnblogs.com/asxinyu/p/3295309.html Matlab与.NET的混合编程,掌握了基本过程,加上一定的开发经验和算法基础,肯 定不难.反之,有时 ...

  4. 找出10000内的素数 CSP

    "Problem: To print in ascending order all primes less than 10000. Use an array of processes, SI ...

  5. 如何在CentOS上找出逐渐耗尽磁盘空间的目录和文件

    起因 随着系统运行,CentOS空间不断减少,对此非常焦虑,到底磁盘空间被哪些新增文件占用了呢? 分析过程,主要使用du命令,逐层找出消耗空间的目录 1.在根目录下检索一下文件的占用情况,执行du命令 ...

  6. C#快速找出磁盘内的所有文件

    本文只针对NTFS格式化的磁盘文件快速检索,速度不是非常快,是让你震惊. 一般用文件遍历的方法检索一个50G的文件夹需要几十分钟甚至一个小时的时间,而用本方法只需几秒. using System; u ...

  7. 利用 Process Monitor 找出某个 Windows 选项所对应的注册表值

    多 时候我们要调整一项 Windows 的功能时只需更改一下注册表即可实现.而很多大家眼中所谓的高手,对 Windows 注册表更是玩得出神入化.难道这些高手把 Windows 注册表都记下来了?答案 ...

  8. 10G个64bit整数,找出中位数

    [10G个64bit整数,找出中位数] 题目:在一个文件中有10G个64bit整数,乱序排列,要求找出中位数.内存限制为2G. 解法:内存限制为2G表面上是限制,实际上是一种提示,在提示我们如何利用2 ...

  9. Java8 Stream()关于在所有用户的所有上传记录中,找出每个用户最新上传记录

    原创文章:转载请标明出处 https://www.cnblogs.com/yunqing/p/9504196.html 首先分析相当于如下,在所有的猫中,每个名字的猫都保留年龄最小的一个 import ...

随机推荐

  1. 圆环自带动画进度条ColorfulRingProgressView

    这是项目中遇到了,我也是借鉴大神的, 下载地址:https://github.com/oooohuhu/ColorfulRingProgressView 我把它导入了github中了,里面有详细的使用 ...

  2. lua(注册c库)

    #include <iostream> #include <string.h> extern "C" { #include "lua-5.2.2/ ...

  3. 【BZOJ3769】spoj 8549 BST again DP(记忆化搜索?)

    [BZOJ3769]spoj 8549 BST again Description 求有多少棵大小为n的深度为h的二叉树.(树根深度为0:左右子树有别:答案对1000000007取模) Input 第 ...

  4. 【BZOJ4004】[JLOI2015]装备购买 贪心+高斯消元

    [BZOJ4004][JLOI2015]装备购买 Description 脸哥最近在玩一款神奇的游戏,这个游戏里有 n 件装备,每件装备有 m 个属性,用向量zi(aj ,.....,am) 表示 ( ...

  5. 【bzoj2226】[Spoj 5971] LCMSum 欧拉函数

    题目描述 Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes the Leas ...

  6. GCD(st表+二分)

    GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  7. EasyNVR完美搭配腾讯云CDN/阿里云CDN进行RTMP、HLS直播加速的使用说明

    1.相关资料入口 腾讯云LVB EasyNVR.com 2.加速说明 2.1. 腾讯LVB加速 2.1.1. 开通服务 腾讯云视频LVB开通入口 2.1.2. 登录进入控制台 腾讯云直播控制台 2.1 ...

  8. Office Web Apps 2013对文档的精细定位

    在一般情况下,我们使用Office Web Apps查看文档都是从第一页开始查看,不过在SharePoint搜索中,我们看到这样的结果: 这是2013搜索的一个新特性,可以深入定位到文档内部,支持PP ...

  9. POJ 1068 Parencodings【水模拟--数括号】

    链接: http://poj.org/problem?id=1068 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#probl ...

  10. Django安装debug tool bar

    1.安装Django Debug Toolbarpip install django-debug-toolbar 2.设置项目的DEBUG属性DEBUG = True 3.INSTALLED_APPS ...