Monday, July 25, 2016

Directory Search using C#


 Hi In this blog I am going to show how to perform directory search using c#.First create a C# windows Form Application Project.And then design  form as show in the figuer.




Include the System.IO namespace and then paste the following  code.

private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFilePath();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error in directory search...:\nCause" + ex.ToString());
            }
        }

        private void OpenFilePath()
        {
            string sfullPath = txtPath.Text;
            string spathwithoutextension = Path.GetFullPath(sfullPath);
            string[] filePaths = Directory.GetFiles(spathwithoutextension,txtExtension.Text, SearchOption.AllDirectories);
            for (int i = 0; i < filePaths.Length; i++)
            {
               listBox1.Items.Add(Path.GetFileName(filePaths[i]) + Environment.NewLine);
            }
            label3.Text = "Total Files:" + listBox1.Items.Count.ToString();

        }


Output:



Download Source Code from here.

Click here for my previous blog.



No comments:

Post a Comment