Click here to Skip to main content
15,886,046 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I have a little difficulty in completing this simple program:

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;

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 searchs and opens with explorer that folder.

Thanks in advanced,
Posted
Comments
Andy Lanng 27-Apr-15 10:59am    
What if there is more than one folder called "drivers"?
Do you want to perform a full explorer search to find them all?
Please explain what this is for so that we may have some context of what you are trying to do. There may be much better ways of achieving the same goal.

Thanks ^_^
Member 11401516 27-Apr-15 11:19am    
Yes i whant to perform a full explorer search to find them all

In your text box txtbox_find, all you need to do is include the sub-directory.
Example:
Windows
Windows\System32
Program Files\Microsoft

I would also change your code to this:
C#
String pathToOpen = System.IO.Path.Combine(@"C:\", txtbox_find.Text);
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo()
{
    FileName = pathToOpen,
    UseShellExecute = true,
    Verb = "open",
});
 
Share this answer
 
Comments
Member 11401516 27-Apr-15 11:22am    
Ok but but if I do not know the path to this subdirectory. I want to search it with explorer.
virusstorm 27-Apr-15 11:34am    
Are you trying to write a search tool?
Dave Kreskowiak 27-Apr-15 11:42am    
Explorer doesn't have a command line option to start a search. You'll have to search the drives yourself to come up with paths that have the search term.
Member 11401516 27-Apr-15 11:50am    
Yes i'm trying to make a search tool
virusstorm 27-Apr-15 12:06pm    
The solution Richard posted will work, but I would strong suggest a more robust option with is to use the Windows Search SDK.
http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=7388
This is something which should be simple - just pass SearchOption.AllDirectories to the EnumerateDirectories method[^].

Unfortunately, it's not that simple. When your code encounters a directory to which it doesn't have access, the EnumerateDirectories method will throw an UnauthorizedAccessException and quit. There's no way to skip that directory and continue with the next one.

As a result, you'll need to write your own method to enumerate the directories. Something like this should work:
C#
public static IEnumerable<DirectoryInfo> SafeEnumerateDirectories(DirectoryInfo root)
{
    if (root == null) throw new ArgumentNullException("root");
    if (!root.Exists) return Enumerable.Empty<DirectoryInfo>();
    return SafeEnumerateDirectoriesCore(root);
}

private static IEnumerable<DirectoryInfo> SafeEnumerateDirectoriesCore(DirectoryInfo root)
{
    var searchPaths = new Stack<DirectoryInfo>();
    searchPaths.Push(root);

    while (searchPaths.Count != 0)
    {
        DirectoryInfo currentPath = searchPaths.Pop();
        yield return currentPath;
        
        DirectoryInfo[] subFolders = null;
        try
        {
            foreach (var subDirectory in currentPath.EnumerateDirectories())
            {
                searchPaths.Push(subDirectory);
            }
        }
        catch (UnauthorizedAccessException)
        {
        }
    }
}

You can then use LINQ to find the matching directories and open them:
C#
string toFind = txtbox_find.Text;
var root = new DirectoryInfo(@"C:\");

var matchingDirectories = SafeEnumerateDirectories(root)
    .Where(d => string.Equals(d.Name, toFind, StringComparison.OrdinalIgnoreCase))
;

foreach (DirectoryInfo directory in matchingDirectories)
{
    Process.Start(new ProcessStartInfo
    {
        FileName = directory.FullName,
        UseShellExecute = true,
        Verb = "open"
    });
}
 
Share this answer
 
v3
Comments
Member 11401516 27-Apr-15 12:24pm    
Thank you very much Richard Deeming. Bu who i implement this in the code that I already have?
Richard Deeming 27-Apr-15 12:28pm    
Add the static methods from the first code block to your class. Replace the contents of your button_browse_Click method with the second code block.

If you need to use the same search methods from other parts of your code, add them to a separate class, and qualify the method call with the class name:

var matchingDirectories = TheClassName.SafeEnumerateDirectories(root) ...
Member 11401516 28-Apr-15 17:15pm    
Thanks for everything the application is running. However the path C: it was only for testing (var root = new DirectoryInfo (@ "C: \");) the goal is that the application search folders on a local server (var root = new DirectoryInfo (@ "\\ vgst \ clients \ ");). Inside the folder clients there are hundreds and hundreds of subdirectories with a unique ID that is not repeated, this ID is the reference sale. Already tested and the application works however is very slow in searching ,sometimes takes several minutes to open the folder that we sent to search. Is there any way to accelerate this search?

Thanks again,
Richard Deeming 29-Apr-15 7:25am    
Are the directories you're looking for all immediate children of the root directory? If so, you could probably use the built-in EnumerateDirectories method, specifying SearchOption.TopDirectoryOnly, and passing a wildcard search pattern for the name:

var matchingDirectories = root.EnumerateDirectories(toFind, SearchOption.TopDirectoryOnly);

That would also let you specify wildcards in the search pattern:
DirectoryInfo.EnumerateDirectories Method : Remarks[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900