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),如有轉載請標明出處,不甚感激!!!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM