类似windows的搜索功能,关键字以文本文件的形式给出,每行一个,这个倒是好实现.
但是怎么检索word文档,excel文档,甚至其他不定类型的文档中的内容呢???

解决方案 »

  1.   

    试试看用StringReader读取每个文件,有些文件还是能读出文本内容的,如word和excel,虽然里面的格式基本上看不懂,不过该有的内容还是有的。
      

  2.   

    是用streamReader读出流,然后与文本文件中读取出的关键字比较么?有没有更详细的说明呢?谢谢
      

  3.   

    http://www.cnblogs.com/onlytiancai/archive/2008/04/29/1176981.html
      

  4.   


                List<string> result = new List<string>();
                foreach (string file in Directory.GetFiles(@"C:\"))
                {
                    using (StreamReader sr = new StreamReader())
                    {
                        if (sr.ReadToEnd().IndexOf("search string") > 0)
                            result.Add(file);
                    }
                }
      

  5.   

    如果你要搜索中文这种,可能需要指定一下StreamReader的Encoding参数
      

  6.   

    如果要搜索子文件夹的内容,需要使用Directory.GetFiles (String, String, SearchOption) 重载
      

  7.   

                List<string> result = new List<string>();
                foreach (string file in Directory.GetFiles(@"C:\"))
                {
                    using (StreamReader sr = new StreamReader(file ))
                    {
                        if (sr.ReadToEnd().IndexOf("search string") > 0)
                            result.Add(file);
                    }
                }
    我代码写漏了,你不会能编译过吧?
    sr.ReadToEnd()会把文件内容都读入内存,所以对于比较大的文件会有问题,比如大于10M的。
    对于这种文件,你可以一行一行读。
      

  8.   


                List<string> result = new List<string>();
                foreach (string file in Directory.GetFiles(@"C:\", "*.*", SearchOption.TopDirectoryOnly))
                {
                    try
                    {
                        using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
                        {
                            using (StreamReader sr = new StreamReader(fs))
                            {
                                if (sr.ReadToEnd().IndexOf("search string") > 0)
                                    result.Add(file);
                            }
                        }
                    }
                    catch
                    {
                    }
                }
      

  9.   

    http://topic.csdn.net/t/20050202/10/3770062.html
      

  10.   

    6楼漏写的部分我自己加上了的,死机倒是没有,但是搜索不出来的。感觉如果是文本文件也许没问题,要是搜索的是word文档的话,就不行了。
      

  11.   

    http://www.dirain.cn/read.php/11.htm自己看,我写过一个文件批量处理器,是不是这个效果。
    是的话从我博客留言,给你代码
      

  12.   

    excel导到数据库里用全文索引呵呵
      

  13.   

    StreamReader sr = new StreamReader("C:\\test.doc", System.Text.Encoding.Unicode);
    word文档貌似是用Unicode编码的,所以这样打开就可以读里面的东西了。