CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2010
    Posts
    31

    Search through a directory.

    I have a sample which I rewrote so that it will only search for mp3 extension files.

    It allows the user to select a drive and then one folder.

    What would be the best to edit it so that it allows the user to search through a specific directory path?

    Code:
    namespace FindFiles
    {
        public partial class Form1 : Form
        {
            
    
            #region private void AddComboBoxItem
            private void AddComboBoxItem(String[] strDrives)
            {
                for (int i = 0; i < strDrives.Length; i++)
                {
                    cmbDrives.Items.Add(strDrives[i]);
                }
            }
            #endregion
    
            #region private void AddListBoxItem
            private void AddListBoxItem()
            {
                listBox1.Items.Add(cmbDrives.Text);
            }
            #endregion
    
            #region private String[] GetDrives
            private String[] GetDrives()
            {
                return Directory.GetLogicalDrives();
            }
            #endregion
    
            #region private Directory[] GetDirectories
            private DirectoryInfo[] GetDirectories(String strDrive)
            {
                DirectoryInfo dir = new DirectoryInfo(strDrive);
                DirectoryInfo[] childDirs = dir.GetDirectories();
                return childDirs;
            }
            #endregion
    
    
            #region public Form1
            public Form1()
            {
                InitializeComponent();
    
                String[] strDrives = GetDrives();
                AddComboBoxItem(strDrives);
    
                // Display the form as a modal dialog box.
                ShowDialog();
            }
            #endregion
    
    
            #region protected void button1_Click
            protected void button1_Click(object sender, System.EventArgs e)
            {
                String strMessage = "No Matches";
                listBox1.Items.Clear();
    
                if (cmbDrives.Text == "")
                {
                    lblHeader.Text = "Select the drive and Enter the file type to be searched!";
                }
               
                else if (cmbDrives.Text == "")
                {
                    lblHeader.Text = "Select the drive to search the files!";
                }
                else
                {
                    try
                    {
                        int i = 0;
                        //int iVal;
                        DirectoryInfo[] ChildDirs = this.GetDirectories(cmbDrives.Text);
    
                        String txtSearchType = "*.mp3";
                        //If the Search file type has no extention then add one.
                        
    
                        //Get the Child Directories.
                        foreach (DirectoryInfo ChildDir in ChildDirs)
                        {
                            if (ChildDir.ToString() == cmbDrivesPrimaryDirectories.SelectedItem.ToString())
                            {
                                //recurse through the child directories.
                                //while (ChildDir.GetDirectories().Length > 0)
                                //{
                                    DirectoryInfo[] grandChilds = ChildDir.GetDirectories();
                                    foreach (DirectoryInfo grandChild in grandChilds)
                                    {
                                        FileInfo[] Files = grandChild.GetFiles(txtSearchType);
                                        if (Files.Length == 0)
                                        {
                                            
                                        }
                                        else
                                        {
                                            foreach (FileInfo DirFile in Files)
                                            {
                                                listBox1.Items.Add(DirFile.Name);
                                                i++;
                                            }
                                        }
                                        //ChildDir = GrandChild;
                                    }
                                //}
                            }
                        }
                    }
                    catch (IOException E)
                    {
                        strMessage = E.Message;
                        listBox1.Items.Add(strMessage);
                        strMessage = "";
                    }
                    if (listBox1.Items.Count == 0)
                    {
                        listBox1.Items.Add(strMessage);
                    }
                }
            }
            #endregion
    
            #region protected void button2_Click
            protected void button2_Click(object sender, System.EventArgs e)
            {
                //Close the list box.
                this.Close();
            }
            #endregion
    
            #region private void cmbDrives_SelectedIndexChanged
            private void cmbDrives_SelectedIndexChanged(object sender, EventArgs e)
            {
    
                DirectoryInfo[] childDirs = this.GetDirectories(cmbDrives.Text);
                foreach (DirectoryInfo childDir in childDirs)
                {
                    cmbDrivesPrimaryDirectories.Items.Add(childDir.ToString());
                }
            }
            #endregion
    
            
        }
    }

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Search through a directory.

    Use the FolderBrowserDialog class.

  3. #3
    Join Date
    Jan 2010
    Posts
    31

    Re: Search through a directory.

    Quote Originally Posted by BigEd781 View Post
    Use the FolderBrowserDialog class.

    How do I use this?

    Code:
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.IO;
    
    public class FolderBrowserDialogExampleForm : System.Windows.Forms.Form
    {
        private FolderBrowserDialog folderBrowserDialog1;
        private OpenFileDialog openFileDialog1;
    
        private RichTextBox richTextBox1;
    
        private MainMenu mainMenu1;
        private MenuItem fileMenuItem, openMenuItem;
        private MenuItem folderMenuItem, closeMenuItem;
    
        private string openFileName, folderName;
    
        private bool fileOpened = false;
    
        // The main entry point for the application.
        [STAThreadAttribute]
        static void Main() 
        {
            Application.Run(new FolderBrowserDialogExampleForm());
        }
    
    
        // Constructor.
        public FolderBrowserDialogExampleForm()
        {
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.fileMenuItem = new System.Windows.Forms.MenuItem();
            this.openMenuItem = new System.Windows.Forms.MenuItem();
            this.folderMenuItem = new System.Windows.Forms.MenuItem();
            this.closeMenuItem = new System.Windows.Forms.MenuItem();
    
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
    
            this.mainMenu1.MenuItems.Add(this.fileMenuItem);
            this.fileMenuItem.MenuItems.AddRange(
                                new System.Windows.Forms.MenuItem[] {this.openMenuItem,
                                                                     this.closeMenuItem,
                                                                     this.folderMenuItem});
            this.fileMenuItem.Text = "File";
    
            this.openMenuItem.Text = "Open...";
            this.openMenuItem.Click += new System.EventHandler(this.openMenuItem_Click);
    
            this.folderMenuItem.Text = "Select Directory...";
            this.folderMenuItem.Click += new System.EventHandler(this.folderMenuItem_Click);
    
            this.closeMenuItem.Text = "Close";
            this.closeMenuItem.Click += new System.EventHandler(this.closeMenuItem_Click);
            this.closeMenuItem.Enabled = false;
    
            this.openFileDialog1.DefaultExt = "rtf";
            this.openFileDialog1.Filter = "rtf files (*.rtf)|*.rtf";
    
            // Set the help text description for the FolderBrowserDialog.
            this.folderBrowserDialog1.Description = 
                "Select the directory that you want to use as the default.";
    
            // Do not allow the user to create new files via the FolderBrowserDialog.
            this.folderBrowserDialog1.ShowNewFolderButton = false;
    
            // Default to the My Documents folder.
            this.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Personal;
    
            this.richTextBox1.AcceptsTab = true;
            this.richTextBox1.Location = new System.Drawing.Point(8, 8);
            this.richTextBox1.Size = new System.Drawing.Size(280, 344);
            this.richTextBox1.Anchor = AnchorStyles.Top | AnchorStyles.Left | 
                                       AnchorStyles.Bottom | AnchorStyles.Right;
    
            this.ClientSize = new System.Drawing.Size(296, 360);
            this.Controls.Add(this.richTextBox1);
            this.Menu = this.mainMenu1;
            this.Text = "RTF Document Browser";
        }
    
        // Bring up a dialog to open a file.
        private void openMenuItem_Click(object sender, System.EventArgs e)
        {
            // If a file is not opened, then set the initial directory to the
            // FolderBrowserDialog.SelectedPath value.
            if (!fileOpened) {
                openFileDialog1.InitialDirectory = folderBrowserDialog1.SelectedPath;
                openFileDialog1.FileName = null;
            }
    
            // Display the openFile dialog.
            DialogResult result = openFileDialog1.ShowDialog();
    
            // OK button was pressed.
            if(result == DialogResult.OK) 
            {
                openFileName = openFileDialog1.FileName;
                try
                {
                    // Output the requested file in richTextBox1.
                    Stream s = openFileDialog1.OpenFile();
                    richTextBox1.LoadFile(s, RichTextBoxStreamType.RichText);
                    s.Close();    
    
                    fileOpened = true;
    
                } 
                catch(Exception exp)
                {
                    MessageBox.Show("An error occurred while attempting to load the file. The error is:" 
                                    + System.Environment.NewLine + exp.ToString() + System.Environment.NewLine);
                    fileOpened = false;
                }
                Invalidate();
    
                closeMenuItem.Enabled = fileOpened;
            }
    
            // Cancel button was pressed.
            else if(result == DialogResult.Cancel) 
            {
                return;
            }
        }
    
    
        // Close the current file.
        private void closeMenuItem_Click(object sender, System.EventArgs e)
        {
            richTextBox1.Text = "";
            fileOpened = false;
    
            closeMenuItem.Enabled = false;
        }
    
    
        // Bring up a dialog to chose a folder path in which to open or save a file.
        private void folderMenuItem_Click(object sender, System.EventArgs e)
        {
            // Show the FolderBrowserDialog.
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if( result == DialogResult.OK )
            {
                folderName = folderBrowserDialog1.SelectedPath;
                if(!fileOpened)
                {
                    // No file is opened, bring up openFileDialog in selected path.
                    openFileDialog1.InitialDirectory = folderName;
                    openFileDialog1.FileName = null;
                    openMenuItem.PerformClick();
                } 
            }
        }
    }
    Do I just change parts of the code, like for "openMenuItem_Click" to "button1_Click" and others?

    Or those this have to be a separate file?

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Search through a directory.

    Well, I am sure that the documentation would help you out if you did a bit of searching. It is just a simple dialog form that allows the user to search for a directory. When they are done you can use a property of the dialog to get the path as a string.

  5. #5
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Search through a directory.

    Quote Originally Posted by TheDarklord View Post
    Do I just change parts of the code, like for "openMenuItem_Click" to "button1_Click" and others?

    Or those this have to be a separate file?
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured