本文整理汇总了C#中Renci.SshNet.SftpClient类的典型用法代码示例。如果您正苦于以下问题:C# SftpClient类的具体用法?C# SftpClient怎么用?C# SftpClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SftpClient类属于Renci.SshNet命名空间,在下文中一共展示了SftpClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CopyFileFromRemoteToLocal
public static void CopyFileFromRemoteToLocal(string host, string user, string password, string localPath, string remotePath)
{
using (SftpClient client = new SftpClient(host, user, password))
{
client.KeepAliveInterval = TimeSpan.FromSeconds(60);
client.ConnectionInfo.Timeout = TimeSpan.FromMinutes(180);
client.OperationTimeout = TimeSpan.FromMinutes(180);
client.Connect();
bool connected = client.IsConnected;
// RunCommand(host, user, password, "sudo chmod 777 -R " + remotePath);
var file = File.OpenWrite(localPath);
client.DownloadFile(remotePath, file);
file.Close();
client.Disconnect();
}
}
示例2: PollHandler
private void PollHandler()
{
var exchange = new Exchange(_processor.Route);
var ipaddress = _processor.UriInformation.GetUriProperty("host");
var username = _processor.UriInformation.GetUriProperty("username");
var password = _processor.UriInformation.GetUriProperty("password");
var destination = _processor.UriInformation.GetUriProperty("destination");
var port = _processor.UriInformation.GetUriProperty<int>("port");
var interval = _processor.UriInformation.GetUriProperty<int>("interval", 100);
var secure = _processor.UriInformation.GetUriProperty<bool>("secure");
do
{
Thread.Sleep(interval);
try
{
if (secure)
{
SftpClient sftp = null;
sftp = new SftpClient(ipaddress, port, username, password);
sftp.Connect();
foreach (var ftpfile in sftp.ListDirectory("."))
{
var destinationFile = Path.Combine(destination, ftpfile.Name);
using (var fs = new FileStream(destinationFile, FileMode.Create))
{
var data = sftp.ReadAllText(ftpfile.FullName);
exchange.InMessage.Body = data;
if (!string.IsNullOrEmpty(data))
{
sftp.DownloadFile(ftpfile.FullName, fs);
}
}
}
}
else
{
ReadFtp(exchange);
}
}
catch (Exception exception)
{
var msg = exception.Message;
}
} while (true);
}
示例3: Uploadfiles
private void Uploadfiles(PasswordConnectionInfo connectionInfo, IEnumerable<AchFileEntity> achFilesToUpload)
{
using (var sftp = new SftpClient(connectionInfo))
{
try
{
sftp.Connect();
foreach (var achfile in achFilesToUpload)
{
using (var stream = new MemoryStream())
{
var fileName = achfile.Name + ".ach";
var writer = new StreamWriter(stream);
// writer.Write(achfile.AchFileBody);
writer.Flush();
stream.Position = 0;
sftp.UploadFile(stream, fileName);
this.Manager.ChangeAchFilesStatus(achfile, AchFileStatus.Uploaded);
this.Manager.UnLock(achfile);
}
}
}
finally
{
sftp.Disconnect();
}
}
}
示例4: sendSFTP
private void sendSFTP(string filePath, string fileName)
{
SftpClient sftp = new SftpClient("194.2.93.194", "userece", "AdminPOCNUC01");
sftp.Connect();
using (FileStream filestream = File.OpenRead(filePath))
{
sftp.UploadFile(filestream, "/"+fileName, null);
sftp.Disconnect();
}
}
示例5: CopyFileFromLocalToRemote
public static void CopyFileFromLocalToRemote(string host, string user, string password, string localPath, string remotePath)
{
using (SftpClient client = new SftpClient(host, user, password))
{
client.KeepAliveInterval = TimeSpan.FromSeconds(60);
client.ConnectionInfo.Timeout = TimeSpan.FromMinutes(180);
client.OperationTimeout = TimeSpan.FromMinutes(180);
client.Connect();
bool connected = client.IsConnected;
// RunCommand(host, user, password, "sudo chmod 777 -R " + remotePath);
FileInfo fi = new FileInfo(localPath);
client.UploadFile(fi.OpenRead(), remotePath + fi.Name, true);
client.Disconnect();
}
}
示例6: GetList
public static IEnumerable<SftpFile> GetList(string host, string folder, string userName, string password)
{
using (var sftp = new SftpClient(host, userName, password))
{
sftp.ErrorOccurred += Sftp_ErrorOccurred;
sftp.Connect();
var toReturn = sftp.ListDirectory(folder).ToList();
sftp.Disconnect();
return toReturn;
}
}
示例7: SshFactory
public SshFactory(String host, String username, String password)
{
_connection = new SshClient(host, username, password);
_connection.Connect();
_sftp = new SftpClient(_connection.ConnectionInfo);
_sftp.Connect();
}
示例8: AddToSftpSessionCollection
public static SftpSession AddToSftpSessionCollection(SftpClient sftpclient, SessionState pssession)
{
//Set initial variables
var obj = new SftpSession();
var sftpSessions = new List<SftpSession>();
var index = 0;
// Retrive existing sessions from the globla variable.
var sessionvar = pssession.PSVariable.GetValue("Global:SFTPSessions") as List<SftpSession>;
// If sessions exist we set the proper index number for them.
if (sessionvar != null && sessionvar.Count > 0)
{
sftpSessions.AddRange(sessionvar);
// Get the SessionId of the last item and count + 1
SftpSession lastSession = sftpSessions[sftpSessions.Count - 1];
index = lastSession.SessionId + 1;
}
// Create the object that will be saved
obj.SessionId = index;
obj.Host = sftpclient.ConnectionInfo.Host;
obj.Session = sftpclient;
sftpSessions.Add(obj);
// Set the Global Variable for the sessions.
pssession.PSVariable.Set((new PSVariable("Global:SFTPSessions", sftpSessions, ScopedItemOptions.AllScope)));
return obj;
}
示例9: SFTPHelper
public SFTPHelper()
{
if (client == null)
{
client = new SftpClient(Host, Port, SFtpUserID, SFtpPassword);
}
}
示例10: TransferPluginSftp
public TransferPluginSftp (ConnectionData data) : base (data)
{
if (data.Password != null)
this.sftp = new SftpClient (data.Host, data.Port, data.User, data.Password);
else
this.sftp = new SftpClient (data.Host, data.Port, data.User, data.PrivateKey);
}
示例11: SFTP
public SFTP(FTPAccount account)
{
this.FTPAccount = account;
if (FTPAccount.UserName.Contains("@"))
{
FTPAccount.UserName = FTPAccount.UserName.Substring(0, FTPAccount.UserName.IndexOf('@'));
}
if (!string.IsNullOrEmpty(FTPAccount.Password) && (string.IsNullOrEmpty(FTPAccount.Keypath)))
{
client = new SftpClient(FTPAccount.Host, FTPAccount.Port, FTPAccount.UserName, FTPAccount.Password);
}
else if (string.IsNullOrEmpty(FTPAccount.Password) && (File.Exists(FTPAccount.Keypath)) && (string.IsNullOrEmpty(FTPAccount.Passphrase)))
{
client = new SftpClient(FTPAccount.Host, FTPAccount.Port, FTPAccount.UserName, new PrivateKeyFile(FTPAccount.Keypath));
}
else if (string.IsNullOrEmpty(FTPAccount.Password) && (File.Exists(FTPAccount.Keypath)) && (!string.IsNullOrEmpty(FTPAccount.Passphrase)))
{
client = new SftpClient(FTPAccount.Host, FTPAccount.Port, FTPAccount.UserName, new PrivateKeyFile(FTPAccount.Keypath, FTPAccount.Passphrase));
}
else
{
//Need to do something here...
DebugHelper.WriteLine("Can't instantiate a SFTP client...");
IsInstantiated = false;
return;
}
IsInstantiated = true;
}
示例12: DownloadFilesWithSftpAsync
private async Task DownloadFilesWithSftpAsync(Endpoint endpoint, string decodedPass)
{
var client = new SftpClient(endpoint.Url, endpoint.Username, decodedPass);
client.Connect();
SftpFile[] filesOnServer =
(await this.ListRemoteDirectoriesAsync(client, endpoint.RemoteDirectory)).Where(
x => !this.excludedListOfFiles.Any(y => x.Name.Equals(y))).ToArray();
var primaryLocalDirectory = endpoint.Destinations.FirstOrDefault(x => x.Type == DestinationType.Primary);
var archiveLocalDirectory = endpoint.Destinations.FirstOrDefault(x => x.Type == DestinationType.Archive);
var primaryAndArchiveAreNotExisting = primaryLocalDirectory == null || archiveLocalDirectory == null;
if (primaryAndArchiveAreNotExisting)
{
progress.ReportLine("You haven't provided the primary or archive folder");
return;
}
var filteredLocalFiles = FilterLocalFiles(filesOnServer, GetFilesListInFolder(primaryLocalDirectory.Name));
await this.GetFilesAsync(filteredLocalFiles, endpoint.RemoteDirectory, primaryLocalDirectory.Name, client).ContinueWith(x => client.Disconnect());
this.ArchiveFiles(GetFilesListInFolder(primaryLocalDirectory.Name), primaryLocalDirectory, archiveLocalDirectory);
}
示例13: GangsSFTPDriver
public GangsSFTPDriver(string host, int port, string username, string password, string mountPoint)
: base(mountPoint, "SFTP")
{
this.host = host;
this.port = port;
this.sftpClient = new SftpClient(host, port, username, password);
}
示例14: SftpForm
public SftpForm(ConnectionInfo connectionInfo)
{
InitializeComponent();
this.connectionInfo = connectionInfo;
this.sftpClient = new SftpClient(this.connectionInfo);
this.sftpClient.Connect();
refresh();
}
示例15: SSHCommunication
public SSHCommunication(string server, string user, string password)
{
Server = server;
User = user;
Password = password;
sftp = new SftpClient(Server, User, Password);
sshClient = new SshClient(Server, User, Password);
}