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 static List getFile(string path, string extName, List lst) { try { string[] directories = Directory.GetDirectories(path); DirectoryInfo directoryInfo = new DirectoryInfo(path); FileInfo[] files = directoryInfo.GetFiles(); if (files.Length != 0 || directories.Length != 0) { FileInfo[] array = files; foreach (FileInfo fileInfo in array) { if (extName.ToLower().IndexOf(fileInfo.Extension.ToLower()) >= 0) { lst.Add(fileInfo); } } string[] array2 = directories; foreach (string path2 in array2) { getFile(path2, extName, lst); } } return lst; } catch (Exception ex) { throw ex; } } /// /// 私有方法,递归获取指定类型文件,包含子文件夹 /// /// /// private void getdir(string path, string extName) { try { string[] directories = Directory.GetDirectories(path); DirectoryInfo directoryInfo = new DirectoryInfo(path); FileInfo[] files = directoryInfo.GetFiles(); if (files.Length != 0 || directories.Length != 0) { string text = ""; string text2 = ""; FileInfo[] array = files; foreach (FileInfo fileInfo in array) { text2 = fileInfo.FullName.Substring(0, fileInfo.FullName.LastIndexOf("\\")); text = text2.Substring(text2.LastIndexOf("\\") + 1); if (fileInfo.FullName.Contains("SN_")) { StreamReader streamReader = new StreamReader(fileInfo.FullName); while (!streamReader.EndOfStream) { string text3 = streamReader.ReadLine(); string text4 = text3.Split(':')[0].Trim(); string text5 = text3.Split(':')[1].Trim(); dh.ExecuteSql("insert into windowsninfo(ws_id,ws_sncode,ws_type,ws_value,ws_indate)values(windowsninfo_seq.nextval,'" + text + "','" + text4 + "','" + text5 + "',sysdate)", "insert"); } streamReader.Close(); } try { string uploadFolder = "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/" + text2.Substring(text2.LastIndexOf("\\") + 1) + "/"; ftp.UpLoadFile(text2, fileInfo.Name, uploadFolder, BackUpFolderPath.Text); } catch (Exception ex) { Console.WriteLine(ex.Message); } } FileInfo[] array2 = files; foreach (FileInfo fileInfo2 in array2) { try { if (!Directory.Exists(BackUpFolderPath.Text + "\\" + text + "\\")) { Directory.CreateDirectory(BackUpFolderPath.Text + "\\" + text + "\\"); } File.Move(fileInfo2.FullName, BackUpFolderPath.Text + "\\" + text + "\\" + fileInfo2.Name); } catch (Exception ex2) { Console.WriteLine(ex2.Message); } } try { Directory.Delete(text2); } catch (Exception) { } string[] array3 = directories; foreach (string path2 in array3) { getdir(path2, extName); } } } catch (Exception ex4) { Console.WriteLine(ex4.Message); } } private void timer1_Tick(object sender, EventArgs e) { try { } catch (Exception ex) { Console.WriteLine(ex.Message); } } 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) { timer1.Start(); } } }