123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- 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();
- public 测试记录解析()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- timer1.Interval = 1000 * 10;
- 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) //当前目录文件或文件夹不为空
- {
- foreach (FileInfo f in file) //显示当前目录所有文件
- {
- if (f.FullName.Contains("SN_"))
- {
-
- Console.WriteLine(f.FullName);
- 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)values(windowsninfo_seq.nextval,'" + f.Name + "','"+type+"','"+value+"')", "insert");
- }
- sr.Close();
- }
- //if (f.Attributes.ToString().IndexOf("Hidden") > -1 && f.Attributes.ToString().IndexOf("System") > -1)
- //{
- //}
- //else
- //{
- // if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
- // {
- // lst.Add(f);
- // }
- //}
- }
- foreach (string d in dir)
- {
- Console.WriteLine(d);
- getdir(d, extName);//递归
- }
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- getFile(FolderPath.Text, ".log");
- }
- /// <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)
- {
- timer1.Start();
- }
- }
- }
|