Visual C#.net 查询windows索引服务目录

转载: http://blog.chinaunix.net/uid-20178201-id-3070321.html
Visual C#.net 查询windows索引服务目录   2012-02-11 11:54:18

分类: WINDOWS

创建一个测试文件夹
  1. 您的计算机 C 驱动器上创建一个新的文件夹。命名文件夹 myCatalogFolder
  2. 启动文本编辑器 (如记事本),然后在一个空白文档中粘贴以下文本:
    这是测试索引服务器查询测试文档,此文件的名称是 IndexText.text。
  3. 将文件另存为 C:\myCatalogFolder\IndexText.txt
 
创建一个索引服务目录
  1. 单击 开始,单击 运行,键入 compmgmt.msc,然后单击 确定
  2. 展开 服务和应用程序
  3. 用鼠标右键单击 索引服务,指向 新建,然后再单击 目录
  4. 在 名称 文本框中键入 TestCatalog
  5. 单击 浏览 找到要用来将放在目录中,的文件夹,然后单击 确定 两次。
  6. 在下 新建目录创建,您会收到以下消息:
    索引服务重新启动之前,编录将保持脱机
    单击 确定
  7. 用鼠标右键单击 索引服务,然后单击 $ 以停止索引服务 停止
  8. 用鼠标右键单击 索引服务,然后单击 $ 开始 以重新启动索引服务。
 
定义目录的作用域创建新编录后,添加想要包括在目录的作用域中的文件夹。范围是包含在目录中并从目录中排除的文件夹集。作用域定义了包含在索引中并被从索引中排除的内容。对于包含或排除的每个文件夹,所有子文件夹都还包括或排除。
  1. 双击 TestCatalog (在"创建测试文件夹"节中创建该目录)。
  2. 用鼠标右键单击 目录,指向 新建,然后再单击 目录
  3. 单击 浏览,找到然后单击添加 (C:\myCatalogFolder\) 所需的文件夹,然后单击 确定

    注意在下 在索引中的包含吗?,单击 
 
创建 ASP.NET Web 应用程序
  1. 启动 Microsoft Visual Studio.net。
  2. 若要创建新的使用 Visual C#.net ASP.NET Web 应用程序 项目。项目 IndexQueryApp 命名。 默认状态下,创建 WebForm1.aspx。
  3. 在设计视图中用鼠标右键单击 WebForm1,然后单击 查看 HTML 源文件
  4. Replace the existing HTML code with following sample code:
    1. <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="IndexQueryApp.WebForm1" %>
    2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    3. <HTML>
    4.    <HEAD>
    5.       <title>WebForm3</title>
    6.       <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    7.       <meta name="CODE_LANGUAGE" Content="C#">
    8.       <meta name="vs_defaultClientScript" content="JavaScript">
    9.       <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    10.    </HEAD>
    11.    <body MS_POSITIONING="GridLayout">
    12.       <form id="WebForm3" method="post" runat="server">
    13.          <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 90px; POSITION: absolute; TOP: 136px" runat="server"></asp:TextBox>
    14.          <asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 328px; POSITION: absolute; TOP: 138px" runat="server"></asp:DataGrid>
    15.          <asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 92px; POSITION: absolute; TOP: 199px" runat="server" Text="Button"></asp:Button>
    16.       </form>
    17.    </body>
    18. </HTML>
  5. 在解决方案资源管理器中,用鼠标右键单击   WebForm1,然后单击   查看代码。将现有代码替换下面的代码示例:

  

  1. using System;
  2. using System.Data;

  3. namespace IndexQueryApp
  4. {
  5.    public class WebForm1 : System.Web.UI.Page
  6.    {
  7.       protected System.Web.UI.WebControls.TextBox TextBox1;
  8.       protected System.Web.UI.WebControls.DataGrid DataGrid1;
  9.       protected System.Web.UI.WebControls.Button Button1;
  10.    
  11.         private void Page_Load(object sender, System.EventArgs e)
  12.         {
  13.             // Put user code to initialize the page here.

  14.         }

  15.         #region Web Form Designer generated code
  16.         override protected void OnInit(EventArgs e)
  17.         {
  18.             //

  19.             // CODEGEN: The ASP.NET Web Form Designer requires this call.

  20.             //

  21.             InitializeComponent();
  22.             base.OnInit(e);
  23.         }
  24.         
  25.         /// <summary>

  26.         /// Required method for Designer support - do not modify

  27.         /// the contents of this method by using the code editor.

  28.         /// </summary>

  29.         private void InitializeComponent()
  30.         { 
  31.                              this.Button1.Click += new System.EventHandler(this.Button1_Click);
  32.                              this.Load += new System.EventHandler(this.Page_Load);
  33.                           }
  34.         #endregion

  35.       private void Button1_Click(object sender, System.EventArgs e)
  36.       {
  37.          // Catalog Name

  38.          string strCatalog = "TestCatalog";
  39.          string strQuery="";

  40.          strQuery = "Select DocTitle,Filename,Size,PATH,URL from Scope() where FREETEXT('" +TextBox1.Text+ "')";
  41.           // TextBox1.Text is the word that you type in the text box to query by using Indexing Service.


  42.          string connstring = "Provider=MSIDXS.1;Integrated Security .='';Data Source="+strCatalog;

  43.          System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connstring); 
  44.          conn.Open();

  45.          System.Data.OleDb.OleDbDataAdapter cmd = new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
  46.         
  47.          System.Data.DataSet testDataSet = new System.Data.DataSet();
  48.         
  49.          cmd.Fill(testDataSet, "SearchResults");
  50.          DataView source = new DataView(testDataSet.Tables[0]);
  51.          DataGrid1.DataSource = source;
  52.          DataGrid1.DataBind();
  53.       } 
  54.   }
  55. }

在 生成 菜单上单击 生成解决方案

运行该应用程序
  1. 在 调试 菜单上单击 $ 开始 以运行该应用程序。
  2. 在文本框中键入查询 word 文档。(您可以键入从 IndexText.txt 文件的任何词)。
  3. 单击 按钮。请注意数据网格将显示,并且包含查询结果从 IndexText.txt 文件。
疑难解答等待要生成索引$ 索引服务自动处理该 TestCatalog 并准备进行索引的单词列表。您在运行的代码示例时您可能收不到结果,因为索引服务需要一些时间来生成索引的快速。

若要验证的索引状态,启动计算机管理 MMC 管理单元单击右边的窗格中的   索引服务  然后验证 TestCatalog   单词表  值是大于零。如果该   Word 列表  值为零,等待索引以生成列表。 索引数据可能已损坏如果搜索没有返回预期的搜索结果,查询索引时,索引数据可能已损坏。若要此问题的疑难解答停止,然后重新启动索引服务重新编制索引的所有文档。若要这样做,请按照下列步骤操作:
  1. 单击 开始,指向 设置,然后单击 控制面板
  2. 双击 管理工具,然后双击 计算机管理
  3. 在控制台树中双击 服务和应用程序
  4. 用鼠标右键单击 索引服务,然后单击 停止
  5. 再次,右键单击 索引服务,然后单击 开始

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值