1244
1244

Reputation: 97

How to check if specific directory exists under a directory?

How can I check if a directory Test exists under the path C:\mypath\is\here?

String[] getAllSubDirs = Directory.GetDirectories(directory, Match, SearchOption.AllDirectories);

foreach (String subDir in getAllSubDirs)
{
    if (!subDir.Contains("test"))
    {
        ListViewItem list = new ListViewItem(subDir);
        list.SubItems.Add("N/A");
        listView.Items.Add(list);
        listView.EnsureVisible(list.Index);
    }
}

I want to print out those directories that do not have a folder named Test present.

Upvotes: 0

Views: 243

Answers (2)

munissor
munissor

Reputation: 3785

Instead of

!subDir.Contains("test") 

do

!Directory.Exists(Path.Combine(subDir, "Test"))

Upvotes: 4

eyossi
eyossi

Reputation: 4330

you can use

Directory.Exists(Path.Combine(subDir, "test"))

or if you just know the full path:

Directory.Exists("C:\mypath\is\here\test")

Upvotes: 2

Related Questions