Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 887
Timothy Rajan Avatar asked Oct 01 '15 01:10

Timothy Rajan


People also ask

Does .NET support SFTP?

NET Core doesn't support native SFTP or SCP protocols. Thus we need to borrow an external SFTP client library. Among a list of SFTP client libraries, SSH.NET is free and works fine for most use cases.

What is SFTP in C#?

#About Rebex SFTP NET is a versatile file-transfer component for . NET languages (such as C# or VB.NET). It enables you to transfer files directly from your application using SFTP, a powerful and secure file-transfer protocol that runs over an SSH session.


Video Answer


1 Answers

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

like image 198
Martin Prikryl Avatar answered Sep 18 '22 13:09

Martin Prikryl