-2

I want to search for a path which contains a specific string.

And i dont know in which path i want to search.

Example:

Search in the whole c:\ drive

String: Setup\State

Should find this path:

C:\Windows\Setup\State

N. Hoyer
  • 11
  • 4

2 Answers2

1

You can use Directory.EnumerateDirectories

var paths = Directory.EnumerateDirectories(".. the path you want to search into..", "*.*", SearchOption.AllDirectories)
                     .Where(path => path.Contains("Setup\State"));

Don't forget the namespaces:

using System.IO;
using System.Linq;
Ahmad Ibrahim
  • 1,915
  • 2
  • 15
  • 32
  • Thank you Ahmad Ibrahim. I think it will work but i now get an error: access of path: "C:\Documents and Settings" denied. How can i solve that and allow it? – N. Hoyer Dec 01 '16 at 19:21
  • @N.Hoyer I don't know, but there're questions about this issue (i.e. http://stackoverflow.com/questions/10963888 and http://stackoverflow.com/questions/4986293) – Ahmad Ibrahim Dec 01 '16 at 19:31
-1

How about:

//checks if directory exists. 
if (Directory.Exists(directpryPath))
        {
            //do something is path exists
        }
  • Er, no. The question is "how do I find a directory where it's name contains a specific string" and not "how do I tell if a directory exists" – stuartd Dec 01 '16 at 18:51