windows 系统调用.Windows索引服务.实列在Web访问索引服务

windows 系统调用.Windows索引服务.实列在Web访问索引服务

[下载代码 / download]

涉及技术

MSIDXS OLDB 驱动程序
取得扩展名对应的 mime 类型( 不知道文件的mime无法设置,Response.ContentType 无法实现下载功能 )
索引服务相关,知识及查询语句


编程背景(假设)


IndexServer 公司重199X 年起建立了一个文档共享服务器(ServerA) ,在此存放各种文档如word,pdf等
但由于现在文件存放过多导致,检索文件非常困难。

所以 IndexServer 公司需要在其共享文件服务器上快速检索、大家共享的文件并、提供Web,
检索下载界面。

编程需求


1. 实现对收藏文件,的全文检索(除了文本文件外还要有 office文件,pdf 等文件的检索功能)
2. 提供下载功能

分析


windows2000 开始ms提供了,索引服务、可以利用,不过默认是不支持 PDF 的。
网上有好多索引服务插件,可以下载可以解决 PDF 无法索引的问题、
如下地质 http://addins.china.msn.com/addins_category_desktop.aspx 就有,还可以用在MS桌面搜索中

步骤一

先学习如何配置索引服务

  1. 打开开始菜单 管理工具 > 计算机管理

  2. 打开 "服务和应用程序"

  3. 在"索引服务"节点处,右键菜单 新建编录,会出现一个对话框
    名称:就是“编录”的名称了(任意输入)
    位置:存放索引文件的位置(选择即可)
    本列中:
    名称:IndexServer
    位置:I:\IndexServer

  4. 这时“索引服务”节点,会出现一个 "IndexServer" 的子节点

  5. 在 “IndexServer” 处右键、添加 > 目录;会出现一个对话框,添加要索引的 "目录" 在录入一个别名
    本列中:
    别名:Favorite
    路径:I:\QuBinDocs\Favorite\
  6. 重新启动索引服务,在“索引服务”节点那里可以,在服务里设置也可以

好了现在文档索引已经开始建立了,不过如果文件比较多的话会比较长时间
还有,windows 默认是对全盘开 索引服务的,会占用很大空间,我个人习惯都删除了自己在配置
自己需要索引的目录。

建立完索引后如果是自己本机使用是不需要编程的,每一个 “编录” 会提供一个查询页面在“编录”的节点下就有

步骤二

开始实现编程、使任何人都可以访问了,其实很简单的

界面效果(可以看出索引服务已经生效了)



代码如下(下载的代码中有一些相关的资源在代码最后的地方)

Web config 配置,编录名

< appSettings >     
        
< add  key ="FileIndexName"  value ="IndexServer"   />         
    
</ appSettings >

ASP.net 页面

< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
        
< table >
            
< tr >
                
< td  style ="width: 100px" >
                    查询编录
</ td >
                
< td  style ="width: 100px" >
                    
< asp:TextBox  ID ="txtIndex"  runat ="server"  ReadOnly ="true"  Text ="<%$ AppSettings:FileIndexName %>" ></ asp:TextBox ></ td >
                
< td  style ="width: 100px" >
                    
&nbsp; </ td >
            
</ tr >
            
< tr >
                
< td  style ="width: 100px" >
                    查询词条
</ td >
                
< td  style ="width: 100px" >
                    
< asp:TextBox  ID ="txtQueryText"  runat ="server" ></ asp:TextBox ></ td >
                
< td  style ="width: 100px" >
                
</ td >
            
</ tr >
            
< tr >
                
< td  style ="width: 100px" >
                
</ td >
                
< td  style ="width: 100px" >
                
</ td >
                
< td  style ="width: 100px" >
                
</ td >
            
</ tr >
        
</ table >
    
    
</ div >
        
< asp:Button  ID ="btnQuery"  runat ="server"  OnClick ="btnQuery_Click"  Text ="Button"   />< br  />
        
< asp:GridView  ID ="GridView1"  runat ="server"  AutoGenerateColumns ="False"  Width ="100%"  DataKeyNames ="Path"  OnRowCommand ="GridView1_RowCommand" >
            
< Columns >
                
< asp:BoundField  DataField ="RANK"  HeaderText ="匹配度"   />

                
< asp:BoundField  DataField ="FILENAME"  HeaderText ="文件名"   />                 
                
< asp:BoundField  DataField ="SIZE"  HeaderText ="大小(byte)"   />
                
< asp:BoundField  DataField ="WRITE"  HeaderText ="最后更新时间"   />
                
< asp:BoundField  DataField ="HITCOUNT"  HeaderText ="匹配字数"   />
                
< asp:ButtonField  HeaderText =""  Text ="下载"  CommandName ="download"   />
            
</ Columns >
        
</ asp:GridView >         
    
</ form >
</ body >


程序查询:

protected   void  btnQuery_Click( object  sender, EventArgs e)
    {
        
string  text  =  txtQueryText.Text.Trim();
        
if  (text.Length  >   0 )
        {
            
using  (OleDbConnection conn  =   new  OleDbConnection( " PROVIDER=MSIDXS;DATA SOURCE= "   +  txtIndex.Text))
            {
                conn.Open();
                
using  (OleDbCommand cmd  =  conn.CreateCommand())
                {
                    cmd.CommandText 
=   string .Format( @" select Rank 
,HitCount
,Filename
,Size
,Write
,PATH
from Scope() where CONTAINS ( '{0}') order by Rank desc,WRITE desc
" , txtQueryText.Text.Trim());

                    
// MSIDXS 不支持参数
                    
// cmd.Parameters.Add("Pram1",OleDbType.VarWChar);
                    
// cmd.Parameters[0].Value = txtQueryText.Text;
                     using  (OleDbDataAdapter da  =   new  OleDbDataAdapter(cmd))
                    {

                        
using  (DataTable dt  =   new  DataTable())
                        {
                            da.Fill(dt);
                            GridView1.DataSource 
=  dt;
                            GridView1.DataBind();
                        }

                    }
                }
            }
        }


    }


下载文件代码:

protected   void  GridView1_RowCommand( object  sender, GridViewCommandEventArgs e)
    {
        
if (e.CommandName.ToLower() ==   " download " )
        {
            GridView gv 
=  sender  as  GridView;
            
if (gv != null )
            {
                
string  path  =  gv.DataKeys[ Convert.ToInt32( e.CommandArgument) ].Values[ 0 ].ToString();
                downloadFile(path);
                
            }
        }
    }

    
private   void  downloadFile( string  FullPath)
    {
        
        
string  extension  =  Path.GetExtension(FullPath);
        
string  fileName  =  Path.GetFileName(FullPath);
        
string  ct = "" ;
        
try
        {
            
// 取得扩展名对应的 mime 类型
            RegistryKey  regKey  =   Registry.ClassesRoot.OpenSubKey(extension);
            ct 
=  regKey.GetValue( " Content Type " as   string ;
        }
        
catch (Exception ex)
        {
            Debug.Write(ex.Message);
        }

        
if  (ct  ==   null // 没取到的就按照文件
            ct  =   " application/octet-stream " ;

        
this .Response.AddHeader( " Content-Disposition " " attachment;filename= "   +  HttpUtility.UrlEncode(fileName));
        
this .Response.ContentType  =  ct;
        
this .Response.WriteFile(FullPath);
        
this .Response.End();    
        
        
    }

 

最后把 I:\QuBinDocs\Favorite\ NTFS 权限设置为 ASPNET 用户可以访问即可.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值