C# get files and folders code example

Example 1: c# get files of type in directory

string[] files = System.IO.Directory.EnumerateFiles(directory, "*.*").ToArray();

Example 2: read folder c#

string mydocpath=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);     
      StringBuilder sb = new StringBuilder();
      foreach (string txtName in Directory.GetFiles(@"D:\Links","*.txt"))
      {
        using (StreamReader sr = new StreamReader(txtName))
        {
          sb.AppendLine(txtName.ToString());
          sb.AppendLine("= = = = = =");
          sb.Append(sr.ReadToEnd());
          sb.AppendLine();
          sb.AppendLine();   
        }
      }
      using (StreamWriter outfile=new StreamWriter(mydocpath + @"\AllTxtFiles.txt"))
      {    
        outfile.Write(sb.ToString());
      }

Example 3: get directory of file c#

using System.IO;

string file = "C:\Documents\file.txt";
Path.GetDirectoryName(file);