How to check if a directory with a specific name exists in C #?

How to check if a folder with a name exists xyzin the specified path (recursively), and if it leaves, then get the 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);
}
+4
source share
1 answer

Use this:

Directory.GetDirectories(root, directoryName, SearchOption.AllDirectories);

where root is the path to run and directoryName is the specific name you are looking for. You can use .Any()to check if it exists and .First()to get the first one.

edited after comment pinkfloydx33

Yes, EnumerateDirectories will be better. Sorry, I'm stuck in .net 3.5 mode at the moment: D, so you are looking for:

Directory.EnumerateDirectories(root, directoryName, SearchOption.AllDirectories).FirstOrDefault(); 

and checking for zero.

+2
source

Source: https://habr.com/ru/post/1688361/


All Articles