| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- 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) { }
- }
- /// <summary>
- /// 私有变量
- /// </summary>
- private List<FileInfo> lst = new List<FileInfo>();
- /// <summary>
- /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
- /// </summary>
- /// <param name="path">文件夹路径</param>
- /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
- /// <returns>List<FileInfo></returns>
- public List<FileInfo> getFile(string path, string extName)
- {
- getdir(path, extName);
- return lst;
- }
- /// <summary>
- /// 私有方法,递归获取指定类型文件,包含子文件夹
- /// </summary>
- /// <param name="path"></param>
- /// <param name="extName"></param>
- 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);
- }
- }
- /// <summary>
- /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
- /// </summary>
- /// <param name="path">文件夹路径</param>
- /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
- /// <returns>List<FileInfo></returns>
- public static List<FileInfo> getFile(string path, string extName, List<FileInfo> 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 = "开始监控";
- }
- }
- }
- }
|