码迷,mamicode.com
首页 > 其他好文 > 详细

检查文件内容

时间:2015-11-12 17:38:46      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

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);
                }
            }

        }

    }

  

检查文件内容

标签:

原文地址:http://www.cnblogs.com/rexfieBlogs/p/4959384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!