728x90 AdSpace

  • Latest News

    [Tips] Get All Files in any folder with Recursive function in C#

    You wanna get all files and folder in any folder. You can easy do it with my below function
    Using Recursive technical skill, you can easy do it by yourself

    A recursive function definition has one or more base cases, meaning input(s) for which the function produces a result trivially (without recurring), and one or more recursive cases, meaning input(s) for which the program recurs (calls itself).

    Get All Files in any folder with Recursive function in C#







    You can download full source from here

    Using Recursive function for get all files in folder with sPath

    Source Code:




    public static List<string> GetAllFilesInFolder(string sDirPath)
    {
    // 1.// Store results in the file results list.
    List<string> result = new List<string>();

    // 2.// Store a stack of our directories.
    Stack<string> stack = new Stack<string>();

    // 3.// Add initial directory.
    stack.Push(sDirPath);

    // 4.// Continue while there are directories to process
    while (stack.Count > 0)
    {
    // A.// Get top directory
    string dir = stack.Pop();

    try
    {
    // B. // Add all files at this directory to the result List
    // get all file with txt and folder
    result.AddRange(Directory.GetFiles(dir, "*.txt"));

    // C. // Add all directories at this directory.
    foreach (string dn in Directory.GetDirectories(dir))
    {
    stack.Push(dn);
    }
    }
    catch
    {
    // D. // Could not open the directory
    }
    }
    return result;
    }


    • Blogger Comments
    • Facebook Comments

    0 comments:

    Post a Comment

    Item Reviewed: [Tips] Get All Files in any folder with Recursive function in C# Rating: 5 Reviewed By: Unknown
    Scroll to Top