2

也许这是一个四部分的问题:

  1. 上传到子目录/ies
  2. 子目录不存在
  3. 使用与本地文件不同的远程文件名
  4. 子目录应该有明确的权限(类似于WinSCP .NET 程序集中的根问题 - 如何在创建目录后设置文件夹权限?

尝试:

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();

抛出异常Cannot create remote file '/some/directory/beneath/root/destination.ext1.ext2.txt'. ---> WinSCP.SessionRemoteException: No such file or directory.

通过在我的临时路径中创建子目录结构并在第一个文件夹上使用,我终于能够通过此处指示的疯狂解决方法创建具有正确权限的子目录:PutFiles

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

但这太不直观了。

4

1 回答 1

1

广告 1) WinSCP 不会(通常)创建上传的目标目录。它必须在上传之前存在。您可以使用 测试是否存在,如果不存在,则使用Session.FileExists创建目录。Session.CreateDirectory当然,如果需要,WinSCP 会创建您正在上传的目录。

广告 3)您在 的remotePath参数中指定不同的目标名称Session.PutFiles

session.PutFiles(@"C:\path\original.txt", "/home/user/newname.txt");

广告 4) 您使用 . 指定上传文件/目录的权限TransferOptions.FilePermissions。请注意,WinSCPx为每个r授予权限的组隐式添加目录权限。因此,当您指定600批量上传的权限时,600用于文件,而700用于目录。如果需要对不同的文件/目录使用不同的权限,需要一一上传。

于 2014-09-23T14:32:53.580 回答