35

I am trying to upload a file using SFTP protocol using C# using SSH.NET library. Below is the code I am using

FileInfo f=new FileInfo("C:\\mdu\\abcd.xml");            
string uploadfile=f.FullName;    
Console.WriteLine(f.Name);
Console.WriteLine("uploadfile"+uploadfile);
var client = new SftpClient(host, port, username, password);
client.Connect();
if(client.IsConnected){
       Console.WriteLine("I AM CONNECTED");
}
var fileStream = new FileStream(uploadfile, FileMode.Open);  
if(fileStream!=null){
            Console.WriteLine("YOU ARE NOT NULL");
}
client.BufferSize = 4 * 1024; 
client.UploadFile(fileStream, f.Name,null);
client.Disconnect();
client.Dispose();

I am able to connect and the filestream is also not NULL. But I am getting PermissionDeniedException while attempting to upload a file.

Unhandled Exception: Renci.SshNet.Common.SftpPermissionDeniedException: Permission denied
   at Renci.SshNet.Sftp.SftpSession.RequestOpen(String path, Flags flags, Boolean nullOnError)
   at Renci.SshNet.SftpClient.InternalUploadFile(Stream input, String path, Flags flags, SftpUploadAsyncResult asyncResult, Action`1 uploadCallback)
   at Renci.SshNet.SftpClient.UploadFile(Stream input, String path, Boolean canOverride, Action`1 uploadCallback)
   at Renci.SshNet.SftpClient.UploadFile(Stream input, String path, Action`1 uploadCallback)
   at movemsi.Program.UploadFile()
   at movemsi.Program.Main(String[] args)

Is there any settings I am missing from the above code. Any help is much appreciated.

4 Answers 4

54

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).

Sign up to request clarification or add additional context in comments.

Comments

16

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();
    }
}

Comments

4

I was getting this error because I was using Path.Combine() for my UploadFile argument, like this:

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

I think it makes the slashes the wrong way around. So just do normal string concatenation like the answer above:

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

Comments

1

I was getting this error because I was attempting to upload a file with the same name as an existing file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.