Click here to Skip to main content
15,888,733 members
Home / Discussions / C#
   

C#

 
AnswerRe: Any one help me about the Recursion Concept Diagramatically. Pin
uspatel4-Jun-13 2:18
professionaluspatel4-Jun-13 2:18 
AnswerRe: Any one help me about the Recursion Concept Diagramatically. Pin
Abhinav S4-Jun-13 3:22
Abhinav S4-Jun-13 3:22 
AnswerRe: Any one help me about the Recursion Concept Diagramatically. Pin
Alan Balkany4-Jun-13 4:51
Alan Balkany4-Jun-13 4:51 
GeneralRe: Any one help me about the Recursion Concept Diagramatically. Pin
Keith Barrow4-Jun-13 6:14
professionalKeith Barrow4-Jun-13 6:14 
AnswerRe: Any one help me about the Recursion Concept Diagramatically. Pin
Amarnath S4-Jun-13 20:24
professionalAmarnath S4-Jun-13 20:24 
AnswerRe: Any one help me about the Recursion Concept Diagramatically. Pin
_Maxxx_5-Jun-13 20:06
professional_Maxxx_5-Jun-13 20:06 
AnswerAny One Can Help me to Upload xml file into database Pin
Member 97011655-Jun-13 23:56
Member 97011655-Jun-13 23:56 
GeneralSearchDirectory Pin
HubSnippets3-Jun-13 0:46
HubSnippets3-Jun-13 0:46 
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.

C#
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.

modified 3-Jun-13 6:59am.

GeneralRe: SearchDirectory Pin
Richard MacCutchan3-Jun-13 1:24
mveRichard MacCutchan3-Jun-13 1:24 
GeneralRe: SearchDirectory Pin
Jasmine25013-Jun-13 12:30
Jasmine25013-Jun-13 12:30 
GeneralRe: SearchDirectory Pin
Richard MacCutchan3-Jun-13 20:50
mveRichard MacCutchan3-Jun-13 20:50 
GeneralRe: SearchDirectory Pin
Jasmine25014-Jun-13 5:49
Jasmine25014-Jun-13 5:49 
GeneralRe: SearchDirectory Pin
Richard MacCutchan4-Jun-13 5:53
mveRichard MacCutchan4-Jun-13 5:53 
GeneralRe: SearchDirectory Pin
Jasmine25015-Jun-13 11:57
Jasmine25015-Jun-13 11:57 
GeneralRe: SearchDirectory Pin
Richard MacCutchan5-Jun-13 21:02
mveRichard MacCutchan5-Jun-13 21:02 
GeneralRe: SearchDirectory Pin
Jasmine25016-Jun-13 5:23
Jasmine25016-Jun-13 5:23 
GeneralRe: SearchDirectory Pin
Richard MacCutchan6-Jun-13 6:47
mveRichard MacCutchan6-Jun-13 6:47 
GeneralRe: SearchDirectory Pin
Jasmine25016-Jun-13 6:58
Jasmine25016-Jun-13 6:58 
GeneralRe: SearchDirectory Pin
Richard MacCutchan6-Jun-13 7:13
mveRichard MacCutchan6-Jun-13 7:13 
GeneralRe: SearchDirectory Pin
Pete O'Hanlon6-Jun-13 22:11
mvePete O'Hanlon6-Jun-13 22:11 
GeneralRe: SearchDirectory Pin
Pete O'Hanlon4-Jun-13 7:07
mvePete O'Hanlon4-Jun-13 7:07 
GeneralRe: SearchDirectory Pin
Jasmine25015-Jun-13 11:58
Jasmine25015-Jun-13 11:58 
GeneralRe: SearchDirectory Pin
Pete O'Hanlon5-Jun-13 12:14
mvePete O'Hanlon5-Jun-13 12:14 
GeneralRe: SearchDirectory Pin
Jasmine25013-Jun-13 12:31
Jasmine25013-Jun-13 12:31 
GeneralRe: SearchDirectory Pin
HubSnippets10-Jun-13 0:47
HubSnippets10-Jun-13 0:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.