【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. SQL SERVER 2008递归

    tab1 表结构: create tab1 ( id int primary key identity(1,1), parentid int not null, name varchar(25) ) ...

  2. 洛谷 4035 [JSOI2008]球形空间产生器

    题目戳这里 一句话题意 给你 n+1 个 n 维点,需要你求出这个n维球的球心.(n<=10) Solution 这个题目N维的话确实不好想,反正三维就已经把我搞懵了,所以只好拿二维类比. 首先 ...

  3. PoC简介

    无线一键通功能,POC(PTT Over Cellular)也称PTT(Push To Talk)功能.PTT:一键通(Push-to-Talk)功能是一种全新的移动技术,可以快速地进行"一 ...

  4. Linux入门基础(四)——磁盘管理

  5. dygraphs for R

    dygraphs一个功能非常强大的处理时间序列的画图包!画出的图在html中打开,鼠标点处,即可得到数据信息.详情见 http://rstudio.github.io/dygraphs/index.h ...

  6. 说说JavaScript 中的new吧

    在其他语言中,new操作符都是用来实例化创建一个对象的,JavaScript 中同样如此,但是它又有一些不同.为了说清楚这个问题我们先来看一下JavaScript 中的类.原型.原型链.继承这些概念吧 ...

  7. 3.09课·········for循环

    for循环:反复执行某段代码.for循环四要素:初始条件,循环条件,循环体,状态改变.for(初始条件;循环条件;状态改变){ 循环体} 若初始条件满足循环条件,则进入循环体,执行完循环体,跳回到状态 ...

  8. Data Structure Linked List: Flattening a Linked List

    http://www.geeksforgeeks.org/flattening-a-linked-list/ #include <iostream> #include <vector ...

  9. 【leetcode刷题笔记】Word Search

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  10. 《UNIX网络编程》daytimetcpcli测试

    对于刚刚接触网络的人来说,<UNIX网络编程>中第一个例子(daytimetcpcli)可能就测试不通过.也许你试着继续向后读来,自己写一个服务程序来解决这个问题,但是daytime服务也 ...