title: Retry SFTP if it fails?
body: <p>I am using SSH.Net to upload but want to retry to SFTP the file if the process fails. I have this code but I don't think this is the best way to handle a retry. What would be the best way to go about handling this?</p>
  
  <pre><code>        var exceptions = new List&lt;Exception&gt;();
          int retryCount = 3;
  
          for (int retry = 0; retry &lt; retryCount; retry++)
          {
              try
              {
                  var filePath = Path.Combine(path, fileName);
  
                  var client = new SftpClient("ftp.site.com", 22, "userName", "password");
  
                  client.Connect();
  
                  if (client.IsConnected)
                  {
                      var fileStream = new FileStream(filePath, FileMode.Open);
                      if (fileStream != null)
                      {
                          client.UploadFile(fileStream, "/fileshare/" + fileName, null);
  
                          client.Disconnect();
                          client.Dispose();
                      }
  
                  }
              }
              catch (Exception ex)
              {
                  exceptions.Add(ex);
                  System.Threading.Thread.Sleep(10000);
              }
  
              if (exceptions.Count == 3)
              {
                  throw exceptions[0];
              }
          }
  </code></pre>