首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SSH.Net库及外卡的使用

SSH.Net库及外卡的使用
EN

Stack Overflow用户
提问于 2014-03-10 18:33:48
回答 1查看 4K关注 0票数 1

我需要使用SSH.NET登录到SFTP,并删除文件夹中的所有文件,但似乎SSH.Net不支持外卡。是真的吗?有办法解决吗?这是我的密码。

代码语言:javascript
复制
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();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-11 23:16:56

我在使用这个库的通配符方面也没有什么好运气。我刚刚创建了一个扩展方法(简化):

代码语言:javascript
复制
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();
    }
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22308447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档