class Program { static List<string> lastFiles = new List<string>(); static List<string> errorfiles = new List<string>(); static void Main(string[] args) { ParseDirectory(GetAppConfig("Path")); ProcessFile(GetAppConfig("CheckKey")); foreach(string s in errorfiles) { Console.WriteLine(s); } Console.WriteLine("down"); Console.ReadLine(); } private static string GetAppConfig(string strKey) { foreach (string key in ConfigurationManager.AppSettings) { if (key == strKey) { return ConfigurationManager.AppSettings[strKey]; } } return null; } private static void ParseDirectory(string path) { string[] dirs = Directory.GetDirectories(path);//得到子目录 IEnumerator iter = dirs.GetEnumerator(); while (iter.MoveNext()) { string str = (string)(iter.Current); ParseDirectory(str); } string[] files = Directory.GetFiles(path); if (files.Length > 0) { var query = (from f in files let fi = new FileInfo(f) orderby fi.LastWriteTime descending select fi.FullName).Take(6);//获取最新6个文件 lastFiles.AddRange(query.ToList()); } } static void ProcessFile(string checkKey) { foreach (string path in lastFiles) { using (StreamReader sr = new StreamReader(path)) { string content = sr.ReadToEnd().ToString(); sr.Close(); if (content.Contains(checkKey)) errorfiles.Add(path); } } } }
时间: 2024-09-29 03:20:53