110,533
社区成员
发帖
与我相关
我的任务
分享
using System;
using Tamir.SharpSsh;
namespace SFTP
{
/// <summary>
/// Summary description for SFTPHelper.
/// </summary>
public class SFTPHelper
{
private SFTPHelper()
{
}
private SshTransferProtocolBase m_sshCp;
public bool Connected
{
get
{
return m_sshCp.Connected;
}
}
public SFTPHelper(SshConnectionInfo connectionInfo)
{
//
//TODO: Check connectionInfo
//
m_sshCp = new Sftp(connectionInfo.Host,connectionInfo.User);
if(connectionInfo.Pass != null)
{
m_sshCp.Password = connectionInfo.Pass;
}
if(connectionInfo.IdentityFile != null)
{
m_sshCp.AddIdentityFile(connectionInfo.IdentityFile );
}
}
public void Connect()
{
if(!m_sshCp.Connected)
{
m_sshCp.Connect();
}
}
public void Close()
{
if(m_sshCp.Connected)
{
m_sshCp.Close();
}
}
public bool Upload(string localPath,string remotePath)
{
try
{
if(!m_sshCp.Connected)
{
m_sshCp.Connect();
}
m_sshCp.Put(localPath, remotePath);
return true;
}
catch
{
return false;
}
}
public bool Download(string remotePath,string localPath)
{
try
{
if(!m_sshCp.Connected)
{
m_sshCp.Connect();
}
m_sshCp.Get(remotePath,localPath);
return true;
}
catch
{
return false;
}
}
}
}