我已经使用FTP创建了文件上传(下面给出的代码)并且它正常工作。但现在我必须做一个文件Upload Using SFTP 请您指导我在VS中使用sftp的库。
class Program
{
static void Main(string[] args)
{
try
{
string path = ConfigurationManager.AppSettings["filepath"];
string[] files = Directory.GetFiles(path, @"*.xlsx");
string FtpServer = ConfigurationManager.AppSettings["ftpurl"];
string Username = ConfigurationManager.AppSettings["username"];
string Password = ConfigurationManager.AppSettings["password"];
string directory = ConfigurationManager.AppSettings["directoryname"];
if (files != null)
{
foreach (string file in files)
{
int port = 22;
FileInfo fi = new FileInfo(file);
string fileName = fi.Name;
string fileurl = path + @"/" + fileName;
string ftpFile = FtpServer + @":" + port + @"/" + directory + @"/" + fileName;
FtpWebRequest myRequest = (FtpWebRequest)FtpWebRequest.Create(ftpFile);
//WebRequest myreq = (WebRequest)WebRequest.Create(ftpFile);
//myreq.Credentials = new NetworkCredential(Username, Password);
myRequest.Credentials = new NetworkCredential(Username, Password);
//WebProxy wb = new WebProxy("http://proxy4.wipro.com:8080");
//wb.Credentials = new NetworkCredential(Username, Password);
//myRequest.Proxy = wb;
myRequest.Method = WebRequestMethods.Ftp.UploadFile;
myRequest.Timeout = 1000000;
myRequest.UseBinary = true;
myRequest.KeepAlive = true;
myRequest.ContentLength = fi.Length;
byte[] buffer = new byte[4097];
int bytes = 0;
int total_bytes = (int)fi.Length;
System.IO.FileStream fs = fi.OpenRead();
System.IO.Stream rs = myRequest.GetRequestStream();
while (total_bytes > 0)
{
bytes = fs.Read(buffer, 0, buffer.Length);
rs.Write(buffer, 0, bytes);
total_bytes = total_bytes - bytes;
}
fs.Close();
rs.Close();
//WebRequest upload = (WebRequest)myreq.GetResponse();
FtpWebResponse uploadResponse = (FtpWebResponse)myRequest.GetResponse();
Console.WriteLine("Upload File Successful");
uploadResponse.Close();
}
}
}
catch (Exception ex)
{
FTPDirectory.logfile.LogInfo("Error in Uploading file in ftp://ftp.xactlycorp.com" + ex.Message);
}
}
}
答案 0 :(得分:0)
您也可以使用http://www.chilkatsoft.com/ssh-sftp-dotnet.asp
我已经使用了很多东西而且非常好。
答案 1 :(得分:-1)
您可以使用名为SharpSSH的东西:
http://www.tamirgal.com/blog/page/SharpSSH.aspx
使用示例:
sftp = new Tamir.SharpSsh.Sftp(ipAddress, username, password);
sftp.Connect();
sftp.Get(sourcePath, destinationPath);
sftp.Close();