Open subdirectories with explorer in C#

Asked

Viewed 590 times

4

I am making an application whose purpose is to search and open a subdirectory inside a folder that is on a server. But what I can do so far is just search and open the folders that are at the root...

My code is like this:

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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
         public Form1()
         {
                 InitializeComponent();
         }
         private void textBox1_TextChanged(object sender, EventArgs e)
         {
         }
         private void button1_Click(object sender, EventArgs e)
         {
                 string filepath = string.Format("C:\\TESTE\\{0}", textBox1.Text, SearchOption.AllDirectories);
                 System.Diagnostics.Process prc = new System.Diagnostics.Process();
                 prc.StartInfo.FileName = filepath;
                 prc.Start();
         }

}
}

I know I have to use a recursive function to go through all the folders but I am not able to fit the function in what I want: the user inserts in the textbox the folder to search, press the search button and open and if there opens with the explorer that folder:

public void GetSubDirectories()
{
string root = @"C:\teste";

string[] subdirectoryEntries = Directory.GetDirectories(root);

foreach (string subdirectory in subdirectoryEntries)
         LoadSubDirs(subdirectory);
}
private void LoadSubDirs(string dir)
{
Console.WriteLine(dir);
string[] subdirectoryEntries = Directory.GetDirectories(dir);
foreach (string subdirectory in subdirectoryEntries)
{
         LoadSubDirs(subdirectory);
}
}

2 answers

1

To traverse all subdirectories recursively use the following method:

static void DirSearch(string sDir)
   {
       try
       {
           foreach (string d in Directory.GetDirectories(sDir))
           {
               foreach (string f in Directory.GetFiles(d))
               {
                   Console.WriteLine(f);
               }
               DirSearch(d);
           }
       }
       catch (System.Exception excpt)
       {
           Console.WriteLine(excpt.Message);
       }
   }

Reference: link

0

If you need to find all directories and your children can use the method Directory.Getdirectories(...):

string pastaInicial = "C:\teste";
string pastaAEncontrar = "aMinhaPasta";
string[] listaDePastas = Directory.GetDirectories(pastaInicial, pastaAEncontrar, SearchOption.AllDirectories);
// pode verificar se há uma só pasta, se há varias pastas, se não há pastas

The option SearchOption.AllDirectories causes GetDirectories(...) look for the folder to find in the home folder and its descendants.

  • Someone wants to explain why -1?

  • Yeah, I’ve seen it a lot, the folks negative the issue and want the guy to use psionic powers to understand the subjectivity of the gecko ass. I suppose it’s because you don’t answer the part of fitting the method and then open with the Explorer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.