【发布时间】:2023-02-02 16:48:23
【问题描述】:
我需要使用 SSH 实现一个 Linux 文件实用程序类,如下所示:
class LinuxFileOperation
{
private string ip, username, password;
public LinuxFileOperation(string ip, string username, string password)
{
this.ip = ip;
this.username = username;
this.password = password;
}
public void CopyFileOnDevice(string remoteFileNameToBeCopied, string remoteFileNameToBePasted)
{
using (var sshClient = new SshClient(ip, username, password))
{
sshClient.RunCommand($"cp {remoteFileNameToBeCopied} {remoteFileNameToBePasted}");
}
}
public void DeleteFile(string remoteFilePath)
{
using (var sftpClient = new SftpClient(ip, username, password))
{
sftpClient.DeleteFile(remoteFilePath);
}
}
public bool FileExists(string file)
{
using (var sftpClient = new SftpClient(ip, username, password))
{
var fileAttr = sftpClient.GetAttributes(file);
return fileAttr.IsRegularFile;
}
}
public List<string> GetFileList(string fullSearchPath)
{
using (var sftpClient = new SftpClient(ip, username, password))
{
return sftpClient.ListDirectory(fullSearchPath).Select(s => s.FullName).ToList();
}
}
}
几乎所有具有相同代码的方法使用(var sftpClient/sshClient = new SftpClient/SshClient(ip,用户名,密码)). 任何模式都会减少代码?
【问题讨论】:
标签: c# design-patterns ssh