zoukankan      html  css  js  c++  java
  • 搜索文件或文件内容

    public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
    
            string strkey = context.Request["key"];
    
            string path = context.Server.MapPath("~/news_html");
            List<FileInfo> listJs = new List<FileInfo>();
            if (!string.IsNullOrWhiteSpace(strkey))
            {
                GetFiles(path, (FileInfo file) =>
                {
                    if (file.Extension.Equals(".html") && file.Name.Contains(strkey))
                    {
                        listJs.Add(file);
                    }
                });
            }
            else
            {
                GetFiles(path, (FileInfo file) =>
                {
                    if (file.Extension.Equals(".html"))
                    {
    
                        listJs.Add(file);
                    }
                });
            }
            context.Response.Write(JsonConvert.SerializeObject(listJs));
        }
    
        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <param name="fileRule"></param>
        public void GetFiles(string path, Action<FileInfo> fileRule)
        {
            Queue<string> queue = new Queue<string>();
            queue.Enqueue(path);
            while (queue.Count > 0)
            {
                DirectoryInfo dirInfo = new DirectoryInfo(queue.Dequeue());
                foreach (DirectoryInfo dirchildInfo in dirInfo.GetDirectories())
                {
                    queue.Enqueue(dirchildInfo.FullName);
                }
    
                foreach (FileInfo dirfileInfo in dirInfo.GetFiles())
                {
                    fileRule(dirfileInfo);
                }
            }
        }
    
    
        /// <summary>
        /// 从文件中找关键字
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="word"></param>
        public void FindInFile(string filename, string word)
        {
            System.IO.StreamReader sr = System.IO.File.OpenText(filename);
            string s = sr.ReadToEnd();
            sr.Close();
            string[] temp = s.Split('
    ');
            for (int i = 0; i < temp.Length; i++)
            {
                if (temp[i].IndexOf(word) != -1)
                {
                    Console.WriteLine(string.Format(
                    "Found in: {0}
    {1}
    Line: {2} 
    ",
                    filename, temp[i].Trim(), i + 1));
                }
            }
        }
        /// <summary>
        /// 从文件夹中找关键字
        /// </summary>
        /// <param name="foldername"></param>
        /// <param name="word"></param>
        public void FindInDirectory(string foldername, string word)
        {
            System.IO.DirectoryInfo dif = new System.IO.DirectoryInfo(foldername);
            //遍历文件夹中的各子文件夹
            foreach (System.IO.DirectoryInfo di in dif.GetDirectories())
            {
                FindInDirectory(di.FullName, word);
            }
            //查询文件夹中的各个文件
            foreach (System.IO.FileInfo f in dif.GetFiles())
            {
                FindInFile(f.FullName, word);
            }
        }
    
        public bool IsReusable {
            get {
                return false;
            }
        }
  • 相关阅读:
    Asp.Net异步页处理
    iframe通过js自动调节高度
    Js 代码
    JavaScript使用面向对象的技术创建高级 Web 应用程序 (转)
    C#泛型深入浅出(转)
    Asp.Net异步页面处理(转)
    ASP.NET分页存储过程自定义用户控件(转)
    .NET Remoting程序开发入门篇(转)
    认识WebService (转)
    Java CompletableFuture 详解
  • 原文地址:https://www.cnblogs.com/prolion/p/7753722.html
Copyright © 2011-2022 走看看