using Microsoft.Win32; using System; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.IO; using System.Text; using System.Threading; using System.Windows.Forms; namespace FileWatcher { public partial class 测试记录解析 : Form { DataHelper dh = new DataHelper(); ftpOperater ftp = new ftpOperater(); public 测试记录解析() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { timer1.Interval = 1000 * 10; OperateResult.AppendText("连接上服务器\n"); //timer1.Start(); } public void DoLog(string Message) { try { StreamWriter sw = File.AppendText(DateTime.Now.ToString("yyyy-MM-dd") + ".txt"); sw.WriteLine("\n【时间】" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Message + "\n"); sw.Close(); } catch (Exception) { } } /// /// 私有变量 /// private List lst = new List(); /// /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹) /// /// 文件夹路径 /// 扩展名可以多个 例如 .mp3.wma.rm /// List public List getFile(string path, string extName) { getdir(path, extName); return lst; } /// /// 私有方法,递归获取指定类型文件,包含子文件夹 /// /// /// private void getdir(string path, string extName) { try { string[] dir = Directory.GetDirectories(path); //文件夹列表 DirectoryInfo fdir = new DirectoryInfo(path); FileInfo[] file = fdir.GetFiles(); //FileInfo[] file = Directory.GetFiles(path); //文件列表 if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空 { string SN = ""; string folderpath = ""; foreach (FileInfo f in file) //显示当前目录所有文件 { folderpath = f.FullName.Substring(0, f.FullName.LastIndexOf(@"\")); SN = folderpath.Substring(folderpath.LastIndexOf(@"\") + 1); if (f.FullName.Contains("SN_")) { StreamReader sr = new StreamReader(f.FullName); while (!sr.EndOfStream) { string val = sr.ReadLine(); string type = val.Split(':')[0].Trim(); string value = val.Split(':')[1].Trim(); dh.ExecuteSql("insert into windowsninfo(ws_id,ws_sncode,ws_type,ws_value,ws_indate)values(windowsninfo_seq.nextval,'" + SN + "','" + type + "','" + value + "',sysdate)", "insert"); } sr.Close(); } try { string ftppath = "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/" + folderpath.Substring(folderpath.LastIndexOf(@"\") + 1) + "/"; ftp.UpLoadFile(folderpath, f.Name, ftppath, BackUpFolderPath.Text); } catch (Exception ex) { Console.WriteLine(ex.Message); } OperateResult.AppendText(f.Name + "解析成功"); } foreach (FileInfo f in file) //显示当前目录所有文件 { try { if (!Directory.Exists(BackUpFolderPath.Text + @"\" + SN + @"\")) { Directory.CreateDirectory(BackUpFolderPath.Text + @"\" + SN + @"\"); } File.Move(f.FullName, BackUpFolderPath.Text + @"\" + SN + @"\" + f.Name); } catch (Exception ex) { Console.WriteLine(ex.Message); } } try { Directory.Delete(folderpath); } catch (Exception) { } foreach (string d in dir) { getdir(d, extName);//递归 } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } private void timer1_Tick(object sender, EventArgs e) { try { getFile(FolderPath.Text, ".log"); } catch (Exception ex) { Console.WriteLine(ex.Message); } } /// /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹) /// /// 文件夹路径 /// 扩展名可以多个 例如 .mp3.wma.rm /// List public static List getFile(string path, string extName, List lst) { try { string[] dir = Directory.GetDirectories(path); //文件夹列表 DirectoryInfo fdir = new DirectoryInfo(path); FileInfo[] file = fdir.GetFiles(); //FileInfo[] file = Directory.GetFiles(path); //文件列表 if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空 { foreach (FileInfo f in file) //显示当前目录所有文件 { if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0) { lst.Add(f); } } foreach (string d in dir) { getFile(d, extName, lst);//递归 } } return lst; } catch (Exception ex) { throw ex; } } private void ChooseFolder_Click(object sender, EventArgs e) { FolderBrowserDialog folder = new FolderBrowserDialog(); folder.Description = "选择监控文件夹"; DialogResult result = folder.ShowDialog(); if (result == DialogResult.OK) { FolderPath.Text = folder.SelectedPath; } } private void ChooseBackUpFolder_Click(object sender, EventArgs e) { FolderBrowserDialog folder = new FolderBrowserDialog(); folder.Description = "选择备份文件夹"; DialogResult result = folder.ShowDialog(); if (result == DialogResult.OK) { BackUpFolderPath.Text = folder.SelectedPath; } } private void Start_Click(object sender, EventArgs e) { if (Start.Text == "开始监控") { timer1.Start(); Start.Text = "停止监控"; } else { timer1.Stop(); Start.Text = "开始监控"; } } } }