Upload files though Renci.SshNet.SftpClient.UploadFile() method

  • Thread starter Thread starter thereisnopatchforhumancruelty
  • Start date Start date
T

thereisnopatchforhumancruelty

Guest
Hi to everyone. I'm implementing a windows service that on OnStart() signal it sends some file through ssh to a server. To test my service, client (the service) and server are in the same machine (my pc). So I set these parameters:

  • serverIP = "127.0.0.1"
  • serverPort = 22
  • serverName = "myusername" (example value)
  • serverPassword = "mypassword" (example value)

so I create my SftpClient:

SftpClient client = new SftpClient(serverIP, serverPort, serverName, serverPassword);

in a try/catch statement:

string remoteDirectory = @"C:\Users\myusername\Desktop\FileFolder";
try
{
client.Connect();
if(!client.Exists(remoteDirectory))
client.Create(remoteDirectory); //directory that will contains the file
FileStream f = new FileStream(path + @"\file1.txt", FileMode.Open);
client.UploadFile(f, remoteDirectory + @"\file1.txt", null);
f = new FileStream(path + @"\file2.txt", FileMode.Open);
client.UploadFile(f, remoteDirectory + @"\file2.txt", null);
f.Close();
client.Disconnect();
} catch(Exception e) {
writeLog(e.Message + " - " e.StackTrace());
}



But I get the no such file error. remoteDirectory\file1.txt not exist after the upload because I thinked that this file will be created after the localdirectory\file1.txt upload. In fact, UploadFile is just like the secure copy thourg ssh.


How can I solve my problem?



EDIT:

Stack Trace:

Message text: Renci.SshNet.Common.SftpPathNotFoundException: No such file
in Renci.SshNet.Sftp.SftpSession.RequestOpen(String path, Flags flags, Boolean nullOnError)
in Renci.SshNet.Sftp.SftpFileStream..ctor(ISftpSession session, String path, FileMode mode, FileAccess access, Int32 bufferSize)
in Renci.SshNet.SftpClient.Create(String path)
in Microsoft.ServiceModel.Samples.Example.sendFiles(Int32 status, String remoteDirectory) in C:\[.......]\Example.cs:riga 260

the riga 260 is:

client.Create(remoteDirectory)


I've changed

client.Create(remoteDirectory)

to

client.CreateDirectory(remoteDirectory)
But it still not working.


----------------------------------

Continue reading...
 
Back
Top