无法在 C# 中使用 SSH.NET 上传文件 SFTP - 权限被拒绝 [英] Unable to upload a file SFTP using SSH.NET in C# - Permission Denied
问题描述
我正在尝试使用 SSH.NET 库使用 C# 使用 SFTP 协议上传文件.下面是我使用的代码
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();
我能够连接并且 filestream
也不是 NULL.但是我在尝试上传文件时收到 PermissionDeniedException
.
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.
推荐答案
您需要指定上传文件的完整路径.
You need to specify a full path to upload the file to.
例如:
client.UploadFile(fileStream, "/home/user/" + f.Name, null);
如果没有路径,SFTP 服务器可能会尝试将文件写入根文件夹或您没有写入权限的其他文件夹(因此 权限被拒绝).
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).
这篇关于无法在 C# 中使用 SSH.NET 上传文件 SFTP - 权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!