Search code examples
c#directorydatadirectory

How do I search for a folder in a folder in c#?


I want to check if a specific folder exists in a folder in c#. Currently I'm using the following code.

        string currentPath = Directory.GetCurrentDirectory();

        foreach (string subDir in Directory.GetDirectories(currentPath))
        {
            if (subDir == currentPath + @"\Database") // if "Database" folder exists
            {
                dbinRoot = true;
            }
        }
        if (dbinRoot == true)
        {
            currentPath = currentPath + @"\Database\SMAStaff.accdb";
        }
        else
        {
            for (int i = 0; i < 2; i++)
            {
                currentPath = currentPath.Remove(currentPath.LastIndexOf("\\"));
            }
            currentPath = currentPath + "\\Database\\SMAStaff.accdb";
        }

I wish to know if there is a better way of doing this ???


Solution

  • you can use:

    Directory.Exists(currentPath + @"\Database")