Reputation: 95
We are writing a console application in C# to upload files, through WinSCP .NET assembly using SFTP protocol, to the file server. 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
. 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());
}
}
}
Upvotes: 2
Views: 7358
Reputation: 202177
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. Use the WinSCP Session.FileExists
and Session.CreateDirectory
methods:
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:
path = /Repository/Scan/Java/ant/UAT
sftppath = /Repository/Scan/Java/ant/UAT/UAT_17-11-2016-19_40_05.zip
Upvotes: 5