Reputation: 503
How do I check whether a folder named xyz
exist in a given path(recursively) and if it exits then get its full path so that I can copy some files from it?
Will something like below work or am I missing something?
if (Directory.Exists(Path.Combine(textBox1.Text, "xyz"))
{
string directoryPath = Path.GetDirectoryName(textBox1.Text);
}
Upvotes: 2
Views: 700
Reputation: 3317
Use this:
Directory.GetDirectories(root, directoryName, SearchOption.AllDirectories);
where root is the path to start in and directoryName is the specific name you're looking for. You can use .Any()
to check if it exists and .First()
to get the first.
edited after pinkfloydx33 comment
Yeah, EnumerateDirectories would be better. Sorry, I'm stuck in .net 3.5 mode at the moment :D so you'd be looking for this:
Directory.EnumerateDirectories(root, directoryName, SearchOption.AllDirectories).FirstOrDefault();
and checking for null.
Upvotes: 2