SoFunction
Updated on 2025-03-07

Complete implementation method of C# traversing folders and their subdirectories

This article describes the complete implementation method of C# traversing folders and their subdirectories. Share it for your reference, as follows:

using System;
using ;
using ;
using ;
using ;
using ;
namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      ("STT");
      string str = @"E:\";
      if (!("\\"))
      {
        str += "\\";
      }
      IList<FileInfo> lst = GetFiles(str);
      if (!(str))
      {
        try
        {
          (str);
        }
        catch(Exception e)
        {
          ();
          ();
          return;
        }
      }
      if ((str + ""))
      {
        (str + "");
      }
      FileInfo file = new FileInfo(str + "");
      if (!)
      {
        ();
      }
      using (StreamWriter outFileWriter = new StreamWriter(str + "", false, Encoding.UTF8))
      {
        StringBuilder sb = new StringBuilder();
        foreach (FileInfo item in lst)
        {
          ("\"");
          ();
          ("\"");
          (",");
          ("\r\n");
        }
        ( - 2, 2);
        (());
        ();
        ();
      }
      ("END");
      ();
    }
    private static void GetDirectorys(string strPath, ref List<string> lstDirect)
    {
      DirectoryInfo diFliles = new DirectoryInfo(strPath);
      DirectoryInfo[] diArr = ();
      //DirectorySecurity directorySecurity = null;
      foreach (DirectoryInfo di in diArr)
      {
        try
        {
          //directorySecurity = new DirectorySecurity(, );
          //if (!)
          //{
          ();
          GetDirectorys(, ref lstDirect);
          //}
        }
        catch
        {
          continue;
        }
      }
    }
    /// <summary>
    /// traverse the current directory and subdirectories    /// </summary>
    /// <param name="strPath">File Path</param>    /// <returns>All files</returns>    private static IList&lt;FileInfo&gt; GetFiles(string strPath)
    {
      List&lt;FileInfo&gt; lstFiles = new List&lt;FileInfo&gt;();
      List&lt;string&gt; lstDirect = new List&lt;string&gt;();
      (strPath);
      DirectoryInfo diFliles = null;
      GetDirectorys(strPath, ref lstDirect);
      foreach (string str in lstDirect)
      {
        try
        {
          diFliles = new DirectoryInfo(str);
          (());
        }
        catch
        {
          continue;
        }
      }
      return lstFiles;
    }
  }
}

For more information about C# related content, please check out the topic of this site:Summary of C# traversal algorithm and skills》、《Summary of thread usage techniques for C# programming》、《Summary of C# operation skills》、《Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Summary of C# array operation skills"and"Introduction to C# object-oriented programming tutorial

I hope this article will be helpful to everyone's C# programming.