using Microsoft.Win32; using System; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.IO; using System.Net; using System.Text; using System.Threading; using System.Windows.Forms; namespace FileWatcher { public partial class 测试记录解析DCW : Form { DataHelper dh = new DataHelper(); ftpOperater ftp = new ftpOperater(); public 测试记录解析DCW() { 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) //显示当前目录所有文件 { string filename = f.FullName; if (filename.Substring(filename.LastIndexOf(".") + 1).ToUpper() == "TXT") { folderpath = f.FullName.Substring(0, f.FullName.LastIndexOf(@"\")); SN = f.Name.Split('.')[0]; try { string ftppath = "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/"; ftp.UpLoadFile(folderpath, f.Name, ftppath, ""); dh.ExecuteSql("insert into STEPTESTDETAIL (std_id,std_sn,std_makecode,std_indate,std_class)select STEPTESTDETAIL_seq.nextval,ms_sncode,ms_makecode,sysdate,'http://81.71.42.91:8099/ftp" + ftppath + f.Name + "' from makeserial where substr(ms_sncode,0,12)='" + SN + "'", "insert"); OperateResult.AppendText(SN+"获取成功\n"); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } foreach (FileInfo f in file) //显示当前目录所有文件 { try { File.Delete(f.FullName); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } private void timer1_Tick(object sender, EventArgs e) { try { getFile(FolderPath.Text, ".txt"); } 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) { timer1.Start(); } } }