2011-03-23 3 views
0

C#에서 특정 문자열을 포함하는 파일을 검색 이 문자열을 검색해야하는 파일 및 디렉토리 목록이 있습니다.는 텍스트 상자에 작성된 문자열을 포함 나는 파일을 검색 허용하는 윈폼 응용 프로그램을 작성하는 것을 시도하고있다

이러한 파일의 대부분은 .doc이고 .xls 입니다. doc에서 검색하는 것이 더 쉬울 수도 있지만 Excel 파일에서는 셀이 다른 인코딩 내가 ++ 메모장을 열어 해당 파일을 "읽기"시도했다, 나는 라틴 문자 세포가 쉽게 찾을 것을 발견하지만, 폴란드어 문자들, 두 바이트 인코딩을했다

검색에 내장 된 창에서

, 아무 문제가 없었다, 몇 가지 테스트 파일에 폴란드어 특수 문자 방법이 있다면 그래서 내 질문은 기본적으로 사용하는

이 포함되어 내 문자열이 있다고 말할 수 있었다 이 응용 프로그램에 대한 Windows 내장 검색 엔진 (내가 쓴 것처럼, 나는 파일 이름을 찾을 필요가있다) 또는 어쩌면 당신은 다른 간단한 아이디어, 어떻게 간단한 멀티 파일 검색을 작성할 수 있습니까?

답변

0

.

+0

나는이 방법을 사용하기로 결정 –

0

체크 아웃 Windows Indexing APIs 사용에이 웹 사이트를. 그것은 ASP.NET을 말하지만 코드는 C#입니다.

발췌문은 아래 :

  string QueryText = "asp alliance"; //The search string 
      string CatalogName = "searchcatalog"; //The name of your Index Server catalog 
      int NumberOfSearchResults = 0; 
      DataSet SearchResults = new DataSet(); 

      //Prevent SQL injection attacks - further security measures are recommended 
      QueryText = QueryText.Replace("'", "''"); 

      //Build the search query 
      string SQL = "SELECT doctitle, vpath, Path, Write, Size, Rank "; 
      SQL += "FROM \"" + CatalogName + "\"..SCOPE() "; 
      SQL += "WHERE"; 
      SQL += " CONTAINS(Contents, '" + QueryText + "') "; 
      SQL += "AND NOT CONTAINS(Path, '\"_vti_\"') "; 
      SQL += "AND NOT CONTAINS(FileName, '\".ascx\" OR \".config\" OR \".css\"') "; 
      SQL += "ORDER BY Rank DESC"; 

      //Connect to Index Server and perform search query 
      try 
      { 
       OleDbConnection IndexServerConnection = new OleDbConnection("Provider=msidxs;"); 
       OleDbCommand dbCommand = new OleDbCommand(SQL, IndexServerConnection); 

       OleDbDataAdapter IndexServerDataAdapter = new OleDbDataAdapter(); 
       IndexServerDataAdapter.SelectCommand = dbCommand; 

       IndexServerDataAdapter.Fill(SearchResults); 
       NumberOfSearchResults = SearchResults.Tables[0].Rows.Count; 
      } 
      catch (Exception ExceptionObject) 
      { 
       //Query failed so display an error message 
       NumberOfSearchResults = 0; 
       LabelNumberOfResults.Text = "Problem with retrieving search results due to: " + ExceptionObject.Message; 
       DataGridSearchResults.Visible = false; 

      }