user1909204
user1909204

Reputation: 45

Search for a folder by its name in c# without specifying the path

I want to search a folder by its name. But I don't know the location of the folder.

Have to get the path of that specific folder.

How Can i do it?

Upvotes: 0

Views: 6679

Answers (3)

David nathan
David nathan

Reputation: 1

the only solution is using recursive search to surf all available folders and sub folders and also to jump access denied paths to have complete list of target result.

Upvotes: 0

Habib
Habib

Reputation: 223187

You have to specify the directory to search for the folder using Directory.GetDirectories Method (String, String, SearchOption)

string[] directories = Directory.GetDirectories(@"c:\",
                                                 "*", 
                                                 SearchOption.AllDirectories);

To get all drives from the computer, use DircotoryInfo.GetDrives and then search in all of them you can try:

DriveInfo[] allDrives = DriveInfo.GetDrives();
List<string> directoryList = new List<string>();
foreach (DriveInfo d in allDrives)
{
    directoryList.AddRange(Directory.GetDirectories(d.Name , "*", SearchOption.AllDirectories));
}

Upvotes: 2

andy
andy

Reputation: 6079

// Only get subdirectories that begin with the letter "p."

string[] dirs = Directory.GetDirectories(@"c:\", "p*");
Console.WriteLine("The number of directories starting with p is {0}.",dirs.Length);
foreach (string dir in dirs) 
{
  Console.WriteLine(dir);
}

Reference - Directory.GetDirectories Method (String, String)

If you dont know the drive then you need to search for all drives by changing the drives available on your system.

Upvotes: 0

Related Questions