Jobine23
Jobine23

Reputation: 130

Check if there is any subdirectory in a folder and return path in c#

The tittle is pretty self explanatory. I want to know how could I get fullpath of a subdirectory if there's a subdirectory in a folder.

Here's what I have to check if there's any subdirectories in the folder. 

        private void button2_Click_1(object sender, EventArgs e)
    {
        bool hasSubfolders;
        string SubFolderFullPath = heyman + "/dump";
        DirectoryInfo directory = new DirectoryInfo(SubFolderFullPath);
        DirectoryInfo[] subdirs = directory.GetDirectories();

        if (subdirs.Length == 0) MessageBox.Show("Dossier dump vide...");

    }

Upvotes: 1

Views: 124

Answers (1)

RizkiDPrast
RizkiDPrast

Reputation: 1725

this method

Directory.GetDirectories(targetDirectory);

will return string[] of subdirectory's path. if you want to check recursively you may add more option

GetDirectories(targetDirectory, System.IO.SearchOption.AllDirectories);

you can check if a subdirectory exist by checking its return value length (just like what you did)

msdn page

Upvotes: 1

Related Questions