我们正在用C#编写一个控制台应用程序,通过使用SFTP协议的WinSCP .NET程序集将文件上传到文件服务器。我能够连接到服务器并将文件放到服务器,但不是在我想要的确切位置。请找到如下代码:
其中
path = \Repository\Scan\Java\ant\UAT
zippath = C:\Temp\UAT_17-11-2016-19_40_05.zip
sftppath = \Repository\Scan\Java\ant\UAT\UAT_17-11-2016-19_40_05.zip
ZIP文件位于Repository
文件夹级别,名称为RepositoryScanJavaantUATUAT_17-11-2016-19_40_05.zip
。如果服务器上不存在这些目录,则不会创建它们。
using (Session session = new Session())
{
session.Open(sessionOptions);
{
if (System.IO.Directory.Exists(path))
{
Console.WriteLine("That path exists already.");
}
else
{
DirectoryInfo di = System.IO.Directory.CreateDirectory(path);
Console.WriteLine(
"The directory was created successfully at {0}.",
System.IO.Directory.GetCreationTime(path));
}
try
{
Console.WriteLine("Put Files in the folder");
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
transferResult = session.PutFiles(zippath, sftppath, false, transferOptions);
transferResult.Check();
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
提前致谢!
答案 0 :(得分:1)
您无法使用System.IO.Directory.Exists
检查是否存在SFTP目录,也无法使用System.IO.Directory.CreateDirectory
在SFTP服务器上创建目录。使用WinSCP Session.FileExists
和Session.CreateDirectory
方法:
if (session.FileExists(path))
{
Console.WriteLine("That path exists already.");
}
else
{
session.CreateDirectory(path);
Console.WriteLine("The directory was created successfully");
}
SFTP路径使用斜杠,而不是反斜杠:
path = /Repository/Scan/Java/ant/UAT
sftppath = /Repository/Scan/Java/ant/UAT/UAT_17-11-2016-19_40_05.zip