المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : SearchDirectory



C# Programming
06-03-2013, 04:51 PM
The Microsoft Windows search "textbox" on the top-right corner of the Window Explorer is not too efficient when searching for files. So, I thought I should write something that would provide an easily way of searching for files in your Windows directories.

There is a small application written to do a quick and smart search of files in a directory. It's still under development and your comments and contributions would be appreciated.

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using System.Data.OleDb;using System.Data.SqlClient; namespace WindowsFormsApplication3{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnBrowse_Click(object sender, EventArgs e) { // Specify a Directory name or path into the File Directory TextBox FolderBrowserDialog openDir = new FolderBrowserDialog(); //Choose a Directory path name and display on the File Direcotry TextBox if (openDir.ShowDialog() == DialogResult.OK) txtBxDir.Text = openDir.SelectedPath; else MessageBox.Show("Make sure you have selected a valid path name"); } public void searchDirectory() { //Check for valid File Type and File Directory if (cmbBx.Text == "") MessageBox.Show("Input a valid file type format"); else if (txtBxDir.Text == "") MessageBox.Show("Input a valid directory pathname"); try { //Search the specified Directory for files of a particular file type String[] arrayFileList = Directory.GetFiles(txtBxDir.Text, cmbBx.Text, SearchOption.AllDirectories); foreach (string file in arrayFileList) { string fileName = file.Substring(file.LastIndexOf("\\") + 1); txtBxResult.Text += fileName + "\r\n"; } } catch (IOException) { MessageBox.Show("Choose the correct file type"); } } private string getTextBoxNoFiles() { string noFiles = txtBxResult.Lines.Length.ToString(); return txtBxNoFiles.Text = noFiles; } private void btnSearch_Click(object sender, EventArgs e) { if (txtBxResult.Text != "") MessageBox.Show("The Result Field is not empty"); else if (txtBxResult.Text == "") { searchDirectory(); getTextBoxNoFiles(); } else MessageBox.Show("Make sure the Result field is Empty"); // progressBar1.Value = txtBxResult.Lines.Length; } private void saveMtd() { //Declaration of string File name string fNames; //Declaration of File Stream Object FileStream fStream; //Declaration of SaveDialog object SaveFileDialog saveFiles = new SaveFileDialog(); DialogResult btnClicked = saveFiles.ShowDialog(); //SaveDialog file type options and SaveDialog Object properties string fileType = "Text file (*.txt)|*.txt| All files (*.*)|*.*"; //| Acrobat Reader File (*.pdf)|*.pdf| HTML Reader (*.chm)|*.chm| All files (*.*)|*.*"; fNames = saveFiles.FileName; saveFiles.Filter = fileType; saveFiles.FilterIndex = 2; saveFiles.CheckPathExists = true; saveFiles.CreatePrompt = true; saveFiles.DefaultExt = ".txt"; saveFiles.CheckFileExists = false; //Check for valid file name and if filename exists if (btnClicked == DialogResult.Cancel) return; if (fNames == "" || fNames == null) MessageBox.Show("Invalid filename", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error); else { try { fStream = new FileStream(fNames, FileMode.Create, FileAccess.Write); string[] fileContent = txtBxResult.Lines; BinaryWriter fbinContent = new BinaryWriter(fStream); //StreamWriter sWrite = new StreamWriter(fStream); for (int i = 0; i < fileContent.Length; i++) { // sWrite.WriteLine(fileContent[i]); fbinContent.Write(fileContent[i] + "\r\n"); // fStream } //close Binary writer object if (fbinContent == null) fbinContent.Close(); //Close File Stream object if (fStream == null) fStream.Close(); } catch (IOException) { MessageBox.Show("Error: File cannot be written or not found", "File Error"); } } } private void button1_Click(object sender, EventArgs e) { saveMtd(); } private void btnClear_Click(object sender, EventArgs e) { txtBxResult.Clear(); } private void load() { //Declaration of an Array of string to take TextBox Content String[] txtBxArr = txtBxResult.Lines; string txtcombo = cmbBx.Text; for (int i = 0; i < txtBxArr.Length; i++) { string cmdLoad = null; //Switch statement that checks for filetype before inserting data into database switch (txtcombo) { case "*.pdf": cmdLoad = "INSERT INTO tblFileType (AcrobatReader)" + "VALUES ('" + txtBxArr[i] + "')"; break; case "*.txt": cmdLoad = "INSERT INTO tblFileType (TextDocuments)" + "VALUES ('" + txtBxArr[i] + "')"; break; case "*.doc": cmdLoad = "INSERT INTO tblFileType (MicrosoftWord)" + "VALUES ('" + txtBxArr[i] + "')"; break; case "*.mp3": cmdLoad = "INSERT INTO tblFileType (Musical)" + "VALUES ('" + txtBxArr[i] + "')"; break; default: break; } try { //DataTable dbTable = ; // new DataTable("tblFileType"); //Create a database connection OleDbConnection loadCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=myFilesdb.mdb;"); loadCon.Open(); // Open connection //Open connection to dataset OleDbCommand dbcom = new OleDbCommand(cmdLoad, loadCon); OleDbDataAdapter dbAdap = new OleDbDataAdapter(); dbAdap.InsertCommand = dbcom; dbAdap.SelectCommand = dbcom; DataSet myFiledbDataSet = new DataSet(); dbAdap.Fill(myFiledbDataSet); loadCon.Close(); } catch { MessageBox.Show("Click the LOAD button again"); } } return; } private void btnLoad_Click(object sender, EventArgs e) { load(); } private void button3_Click(object sender, EventArgs e) { Close(); } private void Form1_Load(object sender, EventArgs e) { } private void txtBxSearch_TextChanged(object sender, EventArgs e) { } private void find() { try { string[] result = txtBxResult.Lines; int j = txtBxResult.Lines.Length; //iterate thru each line of the text box for (int i = 0; i < txtBxResult.Lines.Length; i++) { string found = result[i]; //charNo = 0; //check if each line contains a specified text in the search textbox bool files = found.Contains(txtBxSearch.Text); //found; //string fsearch = found.Substring (txtBxSearch.Text.Length , found.Length ); //int index = found.IndexOf(txtBxSearch.Text, 5, txtBxSearch.Text.Length); if (files) { i++; //int u = txtBxResult.Text.Length; //if yes display the text MessageBox.Show("This book " + found + " is available" + " at position " + i); //int charNo = found.Length - txtBxSearch.Text.Length; txtBxResult.ScrollToCaret(); //for (int k = 0; k < txtBxResult.Lines.Length; k++) //{ int index = found.IndexOf(txtBxSearch.Text, found.Length - txtBxSearch.Text.Length, txtBxSearch.Text.Length); txtBxResult.Select(txtBxSearch.Text.Length + index, txtBxSearch.Text.Length); // } i--; } } } catch (Exception e) { MessageBox.Show(e.Message); } } private void txtBxNoFiles_TextChanged(object sender, EventArgs e) { } private void btnFind_Click(object sender, EventArgs e) { find(); } }}
The "find" method should search within the ResultTextBox then select and highlight it.