我目前遇到这个错误:
Unhandled Exception: System.Net.SocketsExceptions: No such host is known...
不太清楚那是什么意思,但这就是我所做的。
public FileUploader(/*string host, string port, string username, string password,*/ string fileLocation)
{
string temp;
Console.Write("Server URL:");
this.host = Console.ReadLine();
Console.Write("Server Port:");
temp = Console.ReadLine();
this.port = int.Parse(temp);
Console.Write("Username:");
this.username = Console.ReadLine();
Console.Write("Password:");
this.password = ReadPassword();
Console.Write("Folder Location:");
this.fileLocation = Console.ReadLine();
}
public void UploadFile(string pathToFile)
{
var client = new SftpClient(host, port, username, password);
client.Connect();
var fileStream = new FileStream(pathToFile, FileMode.Open);
client.BufferSize = 4 * 1024;
client.UploadFile(fileStream,fileLocation, null);
client.Disconnect();
client.Dispose();
}
最佳答案
当我在主机名 中包含“ftp://”和“sftp://”时,我遇到了类似的问题。在我的搜索中,谷歌将我带到了这里。这是因为我从 FtpWebRequest 切换到 SSH.Net,我的主机值以前是一个 url。
我将以下代码作为工作主机名值的示例。其他人也可以改用 IP 地址。
const string sFtpHost = "someprefix.somedomain.com";
const int sFtpPort = 22;
const string userName = "SomeUser";
const string password = "SomePassword";
// ...
string localFileName = SomeFileName;
string remoteFileName = @"/SomeSubFolder/" + System.IO.Path.GetFileName(localFileName);
using (var sftp = new SftpClient(sFtpHost, sFtpPort, userName, password))
{
sftp.Connect();
using (var file = File.OpenRead(localFileName))
{
sftp.UploadFile(input: file, path: remoteFileName, canOverride: true);
}
sftp.Disconnect();
}
关于c# - 使用sftp将文件上传到服务器主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37791123/