The issues with this code snippet is that a user might not have the rights to do anything but scp.
Calling the "cd" command will only raise an error.
For my purposes, I created an additional method in ScpClient that assumes the directory structure already exists on the remote host.
That way, I can simply copy files over without worrying about any of this. And since it does not split the string, the leading / is not lost in the process (which ultimately leads to the observed behaviour of copying into one's home folder).
This is, no doubt, only a workaround, but it works for me.
Calling the "cd" command will only raise an error.
For my purposes, I created an additional method in ScpClient that assumes the directory structure already exists on the remote host.
That way, I can simply copy files over without worrying about any of this. And since it does not split the string, the leading / is not lost in the process (which ultimately leads to the observed behaviour of copying into one's home folder).
This is, no doubt, only a workaround, but it works for me.
/// <summary>
/// Uploads the specified stream to the remote host.
/// This method assumes that the remote directory structure already exists.
/// </summary>
/// <param name="source">Stream to upload.</param>
/// <param name="path">Remote host file name.</param>
public void UploadFile(Stream source, string path)
{
using (var input = new PipeStream())
using (var channel = this.Session.CreateChannel<ChannelSession>())
{
channel.DataReceived += delegate(object sender, Common.ChannelDataEventArgs e)
{
input.Write(e.Data, 0, e.Data.Length);
input.Flush();
};
channel.Open();
channel.SendExecRequest(string.Format("scp -rt \"{0}\"", path));
this.CheckReturnCode(input);
this.InternalUpload(channel, input, source, path);
this.CheckReturnCode(input);
channel.Close();
}
}