title: Upload file to subdirectory with specific name when subdirectories don't exist?
body: <p>Maybe this is a four-part question:</p>
  
  <ol>
  <li>Upload to subdirectory/ies</li>
  <li>Subdirectories don't exist</li>
  <li>Use different remote filename than local file</li>
  <li>Subdirectories should have explicit permissions (similar to root problem in <a href="https://stackoverflow.com/questions/24726700/winscp-net-assembly-how-to-set-folder-permissions-after-creating-directory">WinSCP .NET assembly - How to set folder permissions after creating directory?</a>)</li>
  </ol>
  
  <p>Attempted:</p>
  
  <pre><code>var localPath = Path.GetTempFileName();
  var remoteFolder = "/some/directory/beneath/root";
  var slash = "/"; // maybe given as '/' or '\'...
  var remotePath = remoteFolder + slash + "destination.ext1.ext2.txt";
  var session = new Session(sessionOptions);
  var result = session.PutFiles(localPath, remotePath, false, new FileTransferOptions { FilePermissions = new FilePermissions { Octal = "700" }, KeepTimestamp..., etc });
  result.Check();
  </code></pre>
  
  <p>throws exception <code>Cannot create remote file '/some/directory/beneath/root/destination.ext1.ext2.txt'. ---&gt; WinSCP.SessionRemoteException: No such file or directory.</code></p>
  
  <p>I was finally able to make the subdirectories with the correct permissions via <a href="https://stackoverflow.com/a/24730695/1037948">the crazy workaround indicated here</a> by creating the subdirectory structure in my temp path and using <code>PutFiles</code> on the <em>first folder</em>:</p>
  
  <pre><code>var tempRoot = Path.GetTempPath();
  var tempPath = Path.Combine(tempRoot, remoteFolder);
  Directory.CreateDirectory(tempPath);
  
  // only need to upload the first segment, PutFiles will magically grab the subfolders too...
  var segment = remoteFolder.Substring(0, remoteFolder.IndexOf(slash, StringComparison.Ordinal));
  if( !this.DoesFolderExist(segment) )
  {
          // here's the workaround...
          try
          {
              this._session.PutFiles(Path.Combine(tempRoot, segment), segment, false, new TransferOptions { FilePermissions = this._transferOptions.FilePermissions }).Check();
          }
          catch (InvalidOperationException)
          {
              // workaround for bug in .NET assembly prior to 5.5.5/5.6.1 beta
              // although I never hit this catch, maybe I've got a new enough version?
          }
  }
  Directory.Delete(tempPath); // finish workaround
  </code></pre>
  
  <p>but this was way too unintuitive.</p>