Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9075721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:55:40+00:00 2026-06-16T18:55:40+00:00

I have the following code so that I can search directories to find files.

  • 0

I have the following code so that I can search directories to find files. Now I want to add a way for users to Save the output to a text file?

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.IO;

    namespace RecursiveSearchCS
    {
        public class Form1 : System.Windows.Forms.Form
        {
            internal System.Windows.Forms.Button btnSearch;
            internal System.Windows.Forms.TextBox txtFile;
            internal System.Windows.Forms.Label lblFile;
            internal System.Windows.Forms.Label lblDirectory;
            internal System.Windows.Forms.ListBox lstFilesFound;
            internal System.Windows.Forms.ComboBox cboDirectory;

            private System.ComponentModel.Container components = null;

            public Form1()
            {

                InitializeComponent();

            }

            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    if (components != null)
                    {
                        components.Dispose();
                    }
                }
                base.Dispose(disposing);
            }


            private void InitializeComponent()
            {
                this.btnSearch = new System.Windows.Forms.Button();
                this.txtFile = new System.Windows.Forms.TextBox();
                this.lblFile = new System.Windows.Forms.Label();
                this.lblDirectory = new System.Windows.Forms.Label();
                this.lstFilesFound = new System.Windows.Forms.ListBox();
                this.cboDirectory = new System.Windows.Forms.ComboBox();
                this.SuspendLayout();
        // 
        // btnSearch
        // 
                this.btnSearch.Location = new System.Drawing.Point(608, 248);
                this.btnSearch.Name = "btnSearch";
                this.btnSearch.Size = new System.Drawing.Size(75, 23);
                this.btnSearch.TabIndex = 0;
                this.btnSearch.Text = "Search";
                this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
        // 
        // txtFile
        // 
                this.txtFile.Location = new System.Drawing.Point(8, 40);
                this.txtFile.Name = "txtFile";
                this.txtFile.Size = new System.Drawing.Size(120, 20);
                this.txtFile.TabIndex = 4;
                this.txtFile.Text = "*.*";
        // 
        // lblFile
        // 
                this.lblFile.Location = new System.Drawing.Point(8, 16);
                this.lblFile.Name = "lblFile";
                this.lblFile.Size = new System.Drawing.Size(144, 16);
                this.lblFile.TabIndex = 5;
                this.lblFile.Text = "Search for files containing:";
        // 
        // lblDirectory
        // 
                this.lblDirectory.Location = new System.Drawing.Point(8, 96);
                this.lblDirectory.Name = "lblDirectory";
                this.lblDirectory.Size = new System.Drawing.Size(120, 23);
                this.lblDirectory.TabIndex = 3;
                this.lblDirectory.Text = "Look In:";
        // 
        // lstFilesFound
        // 
                this.lstFilesFound.Location = new System.Drawing.Point(152, 8);
                this.lstFilesFound.Name = "lstFilesFound";
                this.lstFilesFound.Size = new System.Drawing.Size(528, 225);
                this.lstFilesFound.TabIndex = 1;
        // 
        // cboDirectory
        // 
                this.cboDirectory.DropDownWidth = 112;
                this.cboDirectory.Location = new System.Drawing.Point(8, 128);
                this.cboDirectory.Name = "cboDirectory";
                this.cboDirectory.Size = new System.Drawing.Size(120, 21);
                this.cboDirectory.TabIndex = 2;
                this.cboDirectory.Text = "ComboBox1";
        // 
        // Form1
        // 
                this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                this.ClientSize = new System.Drawing.Size(688, 277);
                this.Controls.Add(this.btnSearch);
                this.Controls.Add(this.txtFile);
                this.Controls.Add(this.lblFile);
                this.Controls.Add(this.lblDirectory);
                this.Controls.Add(this.lstFilesFound);
                this.Controls.Add(this.cboDirectory);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                this.ResumeLayout(false);
                this.PerformLayout();

            }
            #endregion

    /// <summary>
    /// The main entry point for the application
    /// </summary>
            [STAThread]
            static void Main()
            {
                Application.Run(new Form1());
            }

            private void btnSearch_Click(object sender, System.EventArgs e)
            {
                lstFilesFound.Items.Clear();
                txtFile.Enabled = false;
                cboDirectory.Enabled = false;
                btnSearch.Text = "Searching...";
                this.Cursor = Cursors.WaitCursor;
                Application.DoEvents();
                DirSearch(cboDirectory.Text);
                btnSearch.Text = "Search";
                this.Cursor = Cursors.Default;
                txtFile.Enabled = true;
                cboDirectory.Enabled = true;
            }

            private void Form1_Load(object sender, System.EventArgs e)
            {
                cboDirectory.Items.Clear();
                foreach (string s in Directory.GetLogicalDrives())
                {
                    cboDirectory.Items.Add(s);
                }
                cboDirectory.Text = "C:\\";
            }

            void DirSearch(string sDir)
            {
                try
                {
                    foreach (string d in Directory.GetDirectories(sDir))
                    {
                        foreach (string f in Directory.GetFiles(d, txtFile.Text))
                        {
                            lstFilesFound.Items.Add(f);
                        }
                        DirSearch(d);
                    }
                }
                catch (System.Exception excpt)
                {
                    Console.WriteLine(excpt.Message);
                }
            }
        }
    }

Here is also a screenshot of the Application Open:

Open Application Image

I am going to add a save button which saves to a specific location when clicked, how would I go about doing this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:55:41+00:00Added an answer on June 16, 2026 at 6:55 pm

    If I am understanding it correctly, use a simple I/O operation.

       using(StreamWriter writer = new StreamWriter("debug.txt", true))
        {
            foreach (string item in lstFilesFound.Items)
            {
                writer.WriteLine(item.ToString());
            }
        }
    

    Few extra pointers:

    In DirSearch, create a variable for Directory.GetDirectories(sDir). Your present code is causing this thing to calculate in every loop. Look for some more similar refactoring code in other areas.

    var dirs = Directory.GetDirectories(sDir);
    foreach (string d in dirs)
    {
        var files = Directory.GetFiles(d, txtFile.Text);
        foreach (string f in files)
        {
            lstFilesFound.Items.Add(f);
        }
        DirSearch(d);
    }
    

    Hope it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code below, how can I have it modified such that it
i have the following code, and i want to know how can supress the
I have the following code that defines a getParts method to find a given
I have the following code which i am using document.location.search in it.. I want
I have following code that I am compiling in a .NET 4.0 project namespace
I have following code that does not work due to a being a value
I have following code that does not work: I never get to goToFoodDetail .
I have following code snippet that i use to compile class at the run
I have the following code that has @item.ID from razor: <a href=# id=deleteitem(@item.ID)>Delete</a> When
I have the following code that gets the MAC address of an interface: static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.