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),如有转载请标明出处,不甚感激!!!
C#实现根据传入时间段,找出时间段内日期,并生成相对应文件路径的更多相关文章
- Matlab.NET混合编程技巧之——找出Matlab内置函数
原文:[原创]Matlab.NET混合编程技巧之--找出Matlab内置函数 Matlab与.NET的混合编程,掌握了基本过程,加上一定的开发经验和算法基础,肯定不难.反之,有时候一个小错误,可能抓破 ...
- 【原创】Matlab.NET混合编程技巧之找出Matlab内置函数
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 Matlab和C#混合编程文章目录 :[目录]Matlab和C#混合编程文章目录 Matlab与.N ...
- Matlab.NET混编技巧之——找出Matlab内置函数
原文 http://www.cnblogs.com/asxinyu/p/3295309.html Matlab与.NET的混合编程,掌握了基本过程,加上一定的开发经验和算法基础,肯 定不难.反之,有时 ...
- 找出10000内的素数 CSP
"Problem: To print in ascending order all primes less than 10000. Use an array of processes, SI ...
- 如何在CentOS上找出逐渐耗尽磁盘空间的目录和文件
起因 随着系统运行,CentOS空间不断减少,对此非常焦虑,到底磁盘空间被哪些新增文件占用了呢? 分析过程,主要使用du命令,逐层找出消耗空间的目录 1.在根目录下检索一下文件的占用情况,执行du命令 ...
- C#快速找出磁盘内的所有文件
本文只针对NTFS格式化的磁盘文件快速检索,速度不是非常快,是让你震惊. 一般用文件遍历的方法检索一个50G的文件夹需要几十分钟甚至一个小时的时间,而用本方法只需几秒. using System; u ...
- 利用 Process Monitor 找出某个 Windows 选项所对应的注册表值
多 时候我们要调整一项 Windows 的功能时只需更改一下注册表即可实现.而很多大家眼中所谓的高手,对 Windows 注册表更是玩得出神入化.难道这些高手把 Windows 注册表都记下来了?答案 ...
- 10G个64bit整数,找出中位数
[10G个64bit整数,找出中位数] 题目:在一个文件中有10G个64bit整数,乱序排列,要求找出中位数.内存限制为2G. 解法:内存限制为2G表面上是限制,实际上是一种提示,在提示我们如何利用2 ...
- Java8 Stream()关于在所有用户的所有上传记录中,找出每个用户最新上传记录
原创文章:转载请标明出处 https://www.cnblogs.com/yunqing/p/9504196.html 首先分析相当于如下,在所有的猫中,每个名字的猫都保留年龄最小的一个 import ...
随机推荐
- Java数据结构-线性表之顺序表ArrayList
线性表的顺序存储结构.也称为顺序表.指用一段连续的存储单元依次存储线性表中的数据元素. 依据顺序表的特性,我们用数组来实现顺序表,以下是我通过数组实现的Java版本号的顺序表. package com ...
- Obj-C数组以及字符串拼接与分割
本文转载至 http://mobile.51cto.com/iphone-392148.htm Obj-C只是增加了一点“特殊语料”的C语言,所以可以用printf()代替NSLog().但我们建议使 ...
- Command 'java' not found during running appium
Question: When Execution code:driver = new RemoteWebDriver(new Uri("http://127.0.0.1:4723/wd/hu ...
- ArcGIS API for javascript Bookmarks(书签)示例2
1.运行效果图 说明:这篇博文介绍的书签位于地图之上 有关博文中引用的API文件 怎么iis上部署,请参考我前面的博文 2.HTML代码 <!DOCTYPE html> <html ...
- 一些blog地址总结整理:
女神 python之路-网络编程初版:https://www.cnblogs.com/Eva-J/articles/8066842.html python之路-网络编程(重点看这个,更细致):http ...
- POJ 2993 Emag eht htiw Em Pleh【模拟画棋盘】
链接: http://poj.org/problem?id=2993 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#probl ...
- Netty聊天室(2):从0开始实战100w级流量应用
目录 客户端 Client 登录和响应处理 写在前面 客户端的会话管理 客户端的逻辑构成 连接服务器与Session 的创建 Session和 channel 相互绑定 AttributeMap接口的 ...
- 给MDI窗体加背景,解释MakeObjectInstance和CallWindowProc的用法
工程含有1个MDI主窗口和2个子窗口.唯一需要注意的是,每个窗体都有ClientHandle,但只有当自己是MDI主窗体的情况下才有值,否则等于0. unit Unit1; interface use ...
- 又一次发现Oracle太美之awr相关脚本简介
又一次发现Oracle太美之awr相关脚本简介 大家知道在$ORACLE_HOME/rdbms/admin下,有例如以下的相关脚本(我的环境为11.2.0.4.2): [oracle@rh64 ~]$ ...
- tfboys——tensorflow模块学习(一)
Tensorflow的基本使用 TensorFlow 的特点: 使用图 (graph) 来表示计算任务. 在被称之为 会话 (Session) 的上下文 (context) 中执行图. 使用 tens ...