title: Count files in a remote SFTP folder with WinSCP
body: <p>I would like to count all files in a remote folder. I found "offical" WinSCP script that lists every file on the folder. But I would like to count then and save the result to a variable. What's the easiest way?</p>
  
  <pre><code>try
  {
      // Setup session options
      SessionOptions sessionOptions = new SessionOptions
      {
          Protocol = Protocol.Sftp,
          HostName = "example.com",
          UserName = "user",
          Password = "mypassword",
          SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx..."
      };
  
      using (Session session = new Session())
      {
          // Connect
          session.Open(sessionOptions);
  
          RemoteDirectoryInfo directory =
              session.ListDirectory("/subdoms/admin/");
  
          foreach (RemoteFileInfo fileInfo in directory.Files)
          {
              Console.WriteLine(
                  "{0} with size {1}, permissions {2} and last modification at {3}",
                  fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions,
                  fileInfo.LastWriteTime);
          }
      }
  
      return 0;   
  }
  catch (Exception e)
  {
      Console.WriteLine("Error: {0}", e);
      return 1;
  }
  </code></pre>