0

I have a little difficulty in completing this simple program:

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;

namespace Find_directories
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void txtbox_find_TextChanged(object sender, EventArgs e)
        {

        }

        private void button_browse_Click(object sender, EventArgs e)
        {

            String v = txtbox_find.Text;
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo()
            {
                FileName = @"c:\" + v,
                UseShellExecute = true,
                Verb = "open"
            });
        }

    }
}

Currently the application is only opening the directories that are contained in c: root, but what i want is to look in the directory c: and open with the explorer the directory / subdirectory inserted into the textbox (txtbox_find).

Example: I put in the textbox "drivers" click on the browse button and the application searches and opens with explorer that folder.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
BRBL
  • 33
  • 1
  • 6
  • Can you give us an example of what is inside `txtbox_find`? – Jevgeni Geurtsen Apr 27 '15 at 14:59
  • I think to approach this, you may want to store a list of your directories on c: drive so you can loop through them to find "drivers" and pass that path to open a new explorer window? – Harry Apr 27 '15 at 14:59
  • The user places in the textbox the name of the folder that want to search for it and the application must go through all the directories and subdirectories contained in C: looking for her. – BRBL Apr 27 '15 at 15:26
  • Does it matter if more than one location is opened? – Ntellect13 Apr 27 '15 at 16:15
  • No Ntellect13 does not. – BRBL Apr 27 '15 at 16:22
  • Does the answer that I posted below work? – Ntellect13 Apr 27 '15 at 16:56
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). BTW, it's "Thanks in advance", not "Thanks in advanced". – John Saunders May 01 '15 at 04:33

2 Answers2

1

You should use DirectoryInfo to browse through the folders.

also simply try:

Process.Start(@"c:\windows\");
Fjodr
  • 919
  • 13
  • 32
0

This will do it for you.

Create a List of strings globally

 public static List<string> pathsToFind = new List<string>();

Put this in your button click event

string originalPath = @"C:\";
string findPath = txtbox_find.Text;

FolderNames(originalPath);

List<string> paths = pathsToFind;

IEnumerable<string> filteredPaths = paths.Where(x => x.Remove(0,x.LastIndexOf('\\') + 1) == findPath);

foreach (string path in filteredPaths)
{
     System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo()
    {
        FileName = "explorer.exe",
        Arguments = path,
        UseShellExecute = true,
        Verb = "open"
    });
}

Then create an alternate method. Let's just call it FolderNames.

 public static void FolderNames(string path)
    {

        pathsToFind.Add(path);

        DirectoryInfo dir = new DirectoryInfo(path);

        try
        {
            foreach (DirectoryInfo info in dir.GetDirectories())
            {
                folderNames(info.FullName);
            }
        }
        catch (Exception)
        {

        }
    }
Ntellect13
  • 331
  • 1
  • 5