[英]Create directory through C# using WinSCP on SFTP server
We are writing a console application in C# to upload files, through WinSCP .NET assembly using SFTP protocol, to the file server.我们正在用 C# 编写一个控制台应用程序,通过 WinSCP .NET 程序集使用 SFTP 协议将文件上传到文件服务器。 I am able to connect to the server and place files to the server but not at the exact place I want.
我能够连接到服务器并将文件放置到服务器,但不能放在我想要的确切位置。 Please find the code as below:
请找到以下代码:
where哪里
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 file is getting placed at Repository
folder level with name as RepositoryScanJavaantUATUAT_17-11-2016-19_40_05.zip
. ZIP 文件被放置在
Repository
文件夹级别,名称为RepositoryScanJavaantUATUAT_17-11-2016-19_40_05.zip
。 If the directories don't exist on the server they are not getting created.如果服务器上不存在这些目录,则不会创建它们。
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());
}
}
}
You cannot use the System.IO.Directory.Exists
to check an existence of SFTP directory, nor you can use the System.IO.Directory.CreateDirectory
to create a directory on the SFTP server.您不能使用
System.IO.Directory.Exists
来检查 SFTP 目录是否存在,也不能使用System.IO.Directory.CreateDirectory
在 SFTP 服务器上创建目录。 Use the WinSCP Session.FileExists
and Session.CreateDirectory
methods:使用 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 paths use a slash, not a backslash: SFTP 路径使用斜杠,而不是反斜杠:
path = /Repository/Scan/Java/ant/UAT sftppath = /Repository/Scan/Java/ant/UAT/UAT_17-11-2016-19_40_05.zip
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.