use*_*890 1 c# ssh sftp wildcard
我需要使用 SSH.NET 登录到 SFTP 并删除文件夹中的所有文件,但似乎 SSH.Net 不支持通配符。这是真的吗?有办法解决吗?这是我的代码。
using (SftpClient sftpClient = new SftpClient(server, port, username, password))
{
foreach (var d in sftpClient.ConnectionInfo.Encryptions.Where(p => p.Key != "blowfish-cbc").ToList())
{
sftpClient.ConnectionInfo.Encryptions.Remove(d.Key);
}
sftpClient.Connect();
sftpClient.DeleteFile("/outgoing/*.*");
sftpClient.Disconnect();
}
Run Code Online (Sandbox Code Playgroud)
我对使用这个库的通配符也不太走运。我刚刚创建了一个扩展方法(简单化):
public static IEnumerable<SftpFile> ListDirectoryWC(this SftpClient client, string pattern, Func<SftpFile,bool> includeTest=null)
{
string directoryName = (pattern[0] == '/' ? "" : "/") + pattern.Substring(0, pattern.LastIndexOf('/'));
string regexPattern = pattern.Substring(pattern.LastIndexOf('/') + 1)
.Replace(".", "\\.")
.Replace("*", ".*")
.Replace("?", ".");
Regex reg = new Regex('^' + regexPattern + '$');
var results = client.ListDirectory(String.IsNullOrEmpty(directoryName) ? "/" : directoryName)
.Where(e => reg.IsMatch(e.Name) && (includeTest == null || includeTest(e)));
return results;
}
public static void DeleteFileWC(this SftpClient client, string pattern, Func<SftpFile,bool> includeTest=null)
{
foreach(var file in client.ListDirectoryWC(pattern, includeTest))
{
file.Delete();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3481 次 |
| 最近记录: |