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 ...
随机推荐
- 【BZOJ3661】Hungry Rabbit 贪心
[BZOJ3661]Hungry Rabbit Description 可怕的洪水在夏天不期而至,兔子王国遭遇了前所未有的饥荒,它们不得不去外面的森林里寻找食物.为了简化起见,我们假设兔子王国中有n只 ...
- zookeepeer ID生成器 (一)
目录 写在前面 1.1. ZK 的分布式命名服务 1.1.1. 分布式 ID 生成器的类型 UUID方案 1.1.2. ZK生成分布式ID 写在最后 疯狂创客圈 亿级流量 高并发IM 实战 系列 疯狂 ...
- js apply / call 函数
这两个函数的作用是: 将函数绑定到另外一个对象上去运行 用call和apply应用另一个函数(类)以后,当前的函数(类)就具备了另一个函数(类)的方法或是属性,这也能够称之为“继承”. functio ...
- matlab常用的一些程序和功能
~ 去除误匹配算法(matlab) 1.ransac算法 [tform,matchedPoints1,matchedPoints2] = ... estimateGeometricTransfo ...
- Python 面试题(上)
Python语言特性 1 Python的函数参数传递 看两个例子: a = 1 deffun(a): a = 2 fun(a) printa # 1 a = [] deffun(a): a.appen ...
- 中国移动OnetNet云平台 GET指令使用
GET /devices//datastreams/KEY HTTP/1.1 Host: api.heclouds.com api-key: pmWLtnkDBSreKfvg7GsClUXdXa4A ...
- JSP页面EL表达式不解析
问题是这样:在搭建springMVC环境的时候,笔者写了一个简单的Controller如下: @Controller public class HelloController { @RequestMa ...
- Data Structure Array: Maximum sum such that no two elements are adjacent
http://www.geeksforgeeks.org/maximum-sum-such-that-no-two-elements-are-adjacent/ #include <iostre ...
- hbase shell-dml(数据管理指令)
hbase shell数据管理篇: append count delete deleteall get get_counter get_splits incr put scan truncate tr ...
- Android系统Recovery工作原理之使用update.zip升级过程分析(三)【转】
本文转载自:http://blog.csdn.net/mu0206mu/article/details/7464699 以下的篇幅开始分析我们在上两个篇幅中生成的update.zip包在具体更新中所经 ...