【发布时间】:2012-06-09 14:09:20
【问题描述】:
我需要知道一种通过 SFTP 连接到 FTP 站点的方法。我正在使用SharpSSH,但找不到执行该程序的示例。
目前,我已经下载了 SharpSSH .DLL 文件并添加为参考。现在我需要编写可以连接的代码,并从 FTP 服务器上传/下载文件。
我该怎么做?帮助。
更新
代码:
//ip of the local machine and the username and password along with the file to be uploaded via SFTP.
FileUploadUsingSftp("http://Some-sftp-site.com", "username", "password", @"D:\", @"name.txt");
以上代码在Main方法中。
然后;
private static void FileUploadUsingSftp(string FtpAddress, string FtpUserName, string FtpPassword, string FilePath, string FileName)
{
Sftp sftp = null;
try
{
// Create instance for Sftp to upload given files using given credentials
sftp = new Sftp(FtpAddress, FtpUserName, FtpPassword);
// Connect Sftp
sftp.Connect();
// Upload a file
sftp.Put(FilePath + FileName);
// Close the Sftp connection
sftp.Close();
}
finally
{
if (sftp != null)
{
sftp.Close();
}
}
}
【问题讨论】:
-
它是 FTP 站点、SFTP 站点还是 FTPS 站点?它们是 3 种不同的东西。
-
这似乎是 DNS 解析的问题。您的机器能否成功将您的 SFTP 服务器名称解析为 IP 地址?
-
你是不是把用户名和密码改成FTP的用户名和密码了。
-
有人能告诉我我应该为
string FilePath, string FileName传递什么吗?FilePath是在 SFTP 服务器中找到的路径吗?而FileName是要保存在服务器中的文件? -
使用这个 - 太棒了! stackoverflow.com/a/5423005/966609