title: Proceed If File Doesn't Exist
body: <p>I have the following code:</p>
  
  <pre><code>public static void readtext(string pathtoText)
  {
  
      if (File.Exists(pathtoText))
      {   
          string[] lines = System.IO.File.ReadAllLines(pathtoText);
  
          // Display the file contents by using a foreach loop.
          foreach (string line in lines)
          {
              clearPath(line);
          }
      }
      else
      {
          Console.WriteLine("{0} doesn't exist, or isn't a valid text file", pathtoText);
      }
  }
  
  public static void clearPath(string path)
  {
  
      if(Directory.Exists(path))
      {
          int directoryCount = Directory.GetDirectories(path).Length;
  
          if(directoryCount &gt; 0)
          {
              Console.WriteLine("{0} has Subdirectories to Remove", path);
              DirectoryInfo di = new DirectoryInfo(path);
  
              foreach (DirectoryInfo dir in di.GetDirectories())
              {
                  dir.Delete(true); 
              }
          }
          else
          {
              Console.WriteLine("{0} has no directories to remove", path);
          }
  
          int fileCount = Directory.GetFiles(path).Length;
  
          if (fileCount &gt; 0)
          {
              Console.WriteLine("{0} has files to Remove", path);
  
              System.IO.DirectoryInfo di = new DirectoryInfo(path);
  
              foreach (FileInfo file in di.GetFiles())
              {
                  try
                  {
                      file.Delete(); 
                  }
                  catch(System.IO.IOException)
                  {
                      Console.WriteLine("Please Close the following File {0}", file.Name);
                  }
              }
          } 
      }
      else
      {
          Console.WriteLine("Path Doesn't Exist {0}", path);
      }
  }
  </code></pre>
  
  <p>My function reads directories from a text file and passes it to clearPath which checks if the directory exists, and if so, cleans it. My problem is that, if a directory doesn't exist,  it stops the program(it doesn't move to the next directory to check if it exists and clean, the for each loop just stops) . </p>
  
  <p>How do I get it to the next directory even if specific directory doesn't exist? </p>