Unable to upload a file SFTP using SSH.NET in C# - Permission Denied

You need to specify a full path to upload the file to.

For example:

client.UploadFile(fileStream, "/home/user/" + f.Name, null);

Without the path, the SFTP server probably tries to write the file to a root folder or other folder that you do not have a write access to (hence the Permission denied).


You can do this:

FileInfo f = new FileInfo("C:\\mdu\\abcd.xml");            
string uploadfile = f.FullName;    
Console.WriteLine(f.Name);
Console.WriteLine("uploadfile" + uploadfile);

//Passing the sftp host without the "sftp://"
var client = new SftpClient("ftp.example.com", port, username, password);
client.Connect();
if(client.IsConnected)
{
    var fileStream = new FileStream(uploadfile, FileMode.Open);  
    if(fileStream != null)
    {
        //If you have a folder located at sftp://ftp.example.com/share
        //then you can add this like:
        client.UploadFile(fileStream, "/share/" + f.Name,null);
        client.Disconnect();
        client.Dispose();
    }
}