也许这是一个由四部分组成的问题:
尝试:
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.
我终于能够通过the crazy workaround indicated here通过在我的临时路径中创建子目录结构并在第一个文件夹上使用PutFiles
来使用正确的权限创建子目录:< / p>
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
但这太不直观了。
答案 0 :(得分:1)
ad 1)WinSCP(通常)不会创建上传的目标目录。它必须在上传之前存在。您可以使用Session.FileExists
测试存在,并使用Session.CreateDirectory
创建目录,否则创建目录。当然,WinSCP会根据需要创建您上传的目录。
ad 3)您在Session.PutFiles
的remotePath
参数中指定了不同的目标名称:
session.PutFiles(@"C:\path\original.txt", "/home/user/newname.txt");
ad 4)您使用TransferOptions.FilePermissions
指定上传文件/目录的权限。请注意,WinSCP会为每个组的目录隐式添加x
权限,并授予r
权限。因此,当您为批量上载指定600
权限时,600
用于文件,而700
用于目录。如果需要对不同的文件/目录使用不同的权限,则需要逐个上载它们。