123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- //
- using System.IO;
- using System.Net;
- using System.Text.RegularExpressions;
- using TellerSystem.ServiceProxy.Ext.ServiceHelper;
- using Platform.Common.RunningParameters;
-
- namespace TellerSystem.Library.Ext.TradeExtension
- {
- /// <summary>
- /// FTP日志上传备份
- /// </summary>
- /// <author>翟国栋</author>
- /// <date>2013/9/25 9:23:56</date>
- public static class LoggerFTP
- {
- static string FileName = "Logs";
- private static byte[] fileData = null;
- private static string filePath = "";
- private static List<string> localLogPath = new List<string>();
-
- /// <summary>
- /// 判断当前日志文件是否存在没有备份的
- /// </summary>
- /// <param name="file">文件路径</param>
- /// <param name="currentDate">营业日期</param>
- /// <returns>True-存在,False-不存在</returns>
- public static bool IsExistLogFile(FileSystemInfo file, string currentDate)
- {
- if (!file.Exists)
- return false;
- DirectoryInfo dire = file as DirectoryInfo;
- if (dire == null)
- return false;
- FileSystemInfo[] files = dire.GetFileSystemInfos();
- var fileCount = files.TakeWhile(x => Convert.ToDecimal(x.Name) < Convert.ToDecimal(currentDate)).ToList();
- if (fileCount.Count() > 0)
- return true;
- return false;
- }
- /// <summary>
- /// 递归遍历本地日志路径
- /// </summary>
- /// <param name="path">本地日志路径</param>
- /// <returns>日志路径列表</returns>
- private static List<string> getFiles(string path)
- {
- string[] strFileNames = Directory.GetFiles(path);
- string[] strDirectories = Directory.GetDirectories(path);
- foreach (string filename in strFileNames)
- {
- localLogPath.Add(filename);
- }
- foreach (string dir in strDirectories)
- {
- getFiles(dir);
- }
- return localLogPath;
- }
- /// <summary>
- /// 遍历子目录,备份当前日期前的日志文件
- /// </summary>
- /// <param name="file"></param>
- public static void GetFileSystemInfos(FileSystemInfo file)
- {
- localLogPath.Clear();
- if (!file.Exists) return;
- var list = getFiles(file.FullName);
-
- foreach (var item in list)
- {
-
- if (!item.Contains(LoginUserInfo.TradeDate))
- {
- var upLastName = item.Substring(item.IndexOf(FileName), item.Length - item.IndexOf(FileName)).ToString();
- //上传日志文件
- string upPath = string.Empty;
- string[] upPathArray = upLastName.Split('\\');
- for (int i = 0; i < upPathArray.Length; i++)
- {
- if (i < upPathArray.Length - 1)
- {
- upPath += upPathArray[i] + "/";
- }
- }
- fileData = File.ReadAllBytes(item);
- var result = 0.00;
- try
- {
- result = TradeHandle.FtpUpLoad(upLastName.Split('\\').LastOrDefault(), upPath, fileData);
- }
- catch (Exception e)
- {
-
- }
- }
- }
- }
-
- /// <summary>
- /// 清除本地日志文件
- /// </summary>
- public static void ClearFiles(FileSystemInfo file)
- {
- DirectoryInfo dire = file as DirectoryInfo;
- if (dire == null) return;
- FileSystemInfo[] files = dire.GetFileSystemInfos();
- string date = string.Empty;
- if (LoginUserInfo.TradeDate.Length == 8)
- date = LoginUserInfo.TradeDate.Substring(0, 4) + "-" + LoginUserInfo.TradeDate.Substring(4, 2) + "-" + LoginUserInfo.TradeDate.Substring(6, 2);
- DateTime dt = DateTime.Now;
- if (!DateTime.TryParse(date, out dt))
- return;
- string[] dateList = new string[3];
- dateList[0] = LoginUserInfo.TradeDate;
- dateList[1] = string.Format("{0:yyyyMMdd}", dt.AddDays(-1));
- dateList[2] = string.Format("{0:yyyyMMdd}", dt.AddDays(-2));
- for (int i = 0; i < files.Length; i++)
- {
- bool flag = true;
- string finame = files[i].Name;
- string fipath = files[i].FullName;
- for (int y = 0; y < dateList.Length; y++)
- {
- if (files[i].Name == dateList[y])
- {
- flag = false;
- continue;
- }
-
- }
- if (flag)
- {
- DirectoryInfo dirInfo = new DirectoryInfo(fipath);
- //删除目录及其子目录
- dirInfo.Delete(true);
- }
-
-
- }
- }
- }
- }
|