0 votes
by (120 points)

Hi, I'm trying to send a file to an SFTP server. I can connect to the server without a problem, but when it comes to sending the file nothing happens. I suspect it may be the file path although I've tried every combination possible. Could it be a setting on the server itself? I've successfully transferred files using a different SFTP client. Here is my code...

public partial class Form1 : Form
{
    private static String username = "";
    private static String password = "";
    private static String serverAddress = "";
    private static int serverPort = 22;
    private static String filename = "";
    private static String sPath = "";

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {
        filename = "";
        DialogResult result = openFileDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            filename = openFileDialog1.FileName;
            textBox_fileName.Text = filename;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Rebex.Net.Sftp client = new Rebex.Net.Sftp();

        serverAddress = textBox_hostName.Text;
        username = textBox_UserName.Text;
        password = textBox_Password.Text;
        serverPort = Int32.Parse(textBox_Port.Text);
        sPath = textBox_serverPath.Text;
        string sFileName = textBox_fileName.Text;

        try
        {
            client.Connect(serverAddress, serverPort);
            client.Login(username, password);
            client.PutFile(sFileName, sPath);
        }

        catch (Exception exception)
        {

            Console.WriteLine(exception.GetBaseException().ToString());
        }
    }
}
Applies to: Rebex SFTP

1 Answer

+1 vote
by (73.6k points)

Can you please post here the values of sFileName and sPath, so I can confirm you are using correct values.

Please, create communication log as well. It will show whether the data were received and it can point to a cause of the problem. The log can be created like this:

client.LogWriter = new Rebex.FileLogWriter("sftp.log", Rebex.LogLevel.Debug);
by (120 points)
edited by
sFileName ... C:\\Users\\Asus\\Documents\\gtgt.txt
sPath ... /Letters/Transfer

I will add a communication log now, thank you.

Edit: I have an exception saying "-$exception    {"Failure; The specified file is a directory."}    Rebex.Net.SftpException
by (73.6k points)
Thank you for the variables values and the exception.

The problem is obvious. You need to specify `sPath` as file path, not directory path.

The exception says the same: *"The specified file is a directory"*

The documentation of the PutFile method mentions it as well: `<param name="remotePath">The path of the remote file. This cannot be a directory.</param>`

Please change the `sPath` to "/Letters/Transfer/gtgt.txt"

The communication log is not needed now.
by (120 points)
Thank you it works perfect;y now, such a small mistake.
...