locked
c# how can you check if 2 folders in a directory exists. RRS feed

  • Question

  • hello so i wanting to check to see if 2 folders or more with certain names exist if both exist or more as time goes on there will be more 

    but currently something like 

    if(Directory.Exists(Application.startuppath + "//debug//aug")

    {

    messagebox.show("Found");

    }

    the issue is i want to make it so it searches for multiple folders and check if they exist if so do something else do something else but not sure the best way of doing this if anyone could assist or knows of a better way to do this, 

    Would be much appreciated 

    thanks in advance elfenliedtopfan5 

    Wednesday, July 15, 2020 6:49 PM

Answers

  • Here is a simple method

    var foldersList = new List<string>() {"C:\\OED", "C:\\uisides"};
    if (foldersList.All(Directory.Exists))
    {
        // all exists
    }
    else
    {
        // not all exists
    }

    Then if two folders exists under a parent folder

    private void button1_Click(object sender, EventArgs e)
    {
        var foldersList = new List<string>() { "C:\\OED", "DotNetLand","Applications" };
        Console.WriteLine(Exists("C:\\OED", foldersList) ? "True" : "False");
    }
    
    public bool Exists(string parentFolder, List<string> subFolders) => 
        Directory.Exists(parentFolder) && subFolders.All(folder => 
            Directory.Exists(Path.Combine(parentFolder,folder)));



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange




    Wednesday, July 15, 2020 8:10 PM

All replies

  • Here is a simple method

    var foldersList = new List<string>() {"C:\\OED", "C:\\uisides"};
    if (foldersList.All(Directory.Exists))
    {
        // all exists
    }
    else
    {
        // not all exists
    }

    Then if two folders exists under a parent folder

    private void button1_Click(object sender, EventArgs e)
    {
        var foldersList = new List<string>() { "C:\\OED", "DotNetLand","Applications" };
        Console.WriteLine(Exists("C:\\OED", foldersList) ? "True" : "False");
    }
    
    public bool Exists(string parentFolder, List<string> subFolders) => 
        Directory.Exists(parentFolder) && subFolders.All(folder => 
            Directory.Exists(Path.Combine(parentFolder,folder)));



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange




    Wednesday, July 15, 2020 8:10 PM
  • Here is a simple method

    var foldersList = new List<string>() {"C:\\OED", "C:\\uisides"};
    if (foldersList.All(Directory.Exists))
    {
        // all exists
    }
    else
    {
        // not all exists
    }

    Then if two folders exists under a parent folder

    private void button1_Click(object sender, EventArgs e)
    {
        var foldersList = new List<string>() { "C:\\OED", "DotNetLand","Applications" };
        Console.WriteLine(Exists("C:\\OED", foldersList) ? "True" : "False");
    }
    
    public bool Exists(string parentFolder, List<string> subFolders) => 
        Directory.Exists(parentFolder) && subFolders.All(folder => 
            Directory.Exists(Path.Combine(parentFolder,folder)));



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange




    Thank you very much Karen this resolved issue Marked as answer 
    Thursday, July 16, 2020 1:22 AM