首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Tamir.SharpSsh.Sftp区分文件和目录

如何使用Tamir.SharpSsh.Sftp区分文件和目录
EN

Stack Overflow用户
提问于 2015-02-21 21:37:29
回答 1查看 1.7K关注 0票数 1

我正在使用Tamir.SharpSsh.Sftp,想要从服务器获得所有的文件名,然后使用dynatree显示在页面上。

我的方法如下:

代码语言:javascript
复制
 public List<DirectoryItem> ConnectSFTP(string address, string username, string password, Tamir.SharpSsh.Sftp ObjClient,string strfolder)
        {
            List<DirectoryItem> returnValue = new List<DirectoryItem>();
            bool status = false;
            try
            {

                System.Collections.ArrayList list = new System.Collections.ArrayList();
                //Tamir.SharpSsh.Sftp client = new Tamir.SharpSsh.Sftp(address + @"/ToNationwide/ivl00018/", username, password);
                Tamir.SharpSsh.Sftp client = (ObjClient == null) ? new Tamir.SharpSsh.Sftp(address, username, password) : ObjClient;
                client.Connect();
                System.Collections.ArrayList arr = client.GetFileList(strfolder);

                foreach (var item in arr)
                {
                    if (item.ToString() != "." && item.ToString() != "..")
                    {
                        list.Add(item);
                    }

                }

                foreach (string line in list)
                {
                    // Windows FTP Server Response Format
                    // DateCreated    IsDirectory    Name

                    // Parse <DIR>
                    bool isDirectory = client.GetFileList(strfolder+"/"+line).Count > 1;// Here i am confuse how to identify is directory or not ?

                    string name = line.ToString();
                    // Create directory info
                    DirectoryItem item = new DirectoryItem();
                    item.BaseUri = address;
                    item.IsDirectory = isDirectory;
                    item.Name = name;


                    item.Items = item.IsDirectory ? ConnectSFTP(item.AbsolutePath, username, password, client, "/"+line) : null;

                    returnValue.Add(item);
                }

                client.Close();
                status = true;
            }

            catch (Exception ex)
            {
            }
            return returnValue;
        }
    }

所以请帮助任何这种类型的函数来检测文件或文件夹?

EN

回答 1

Stack Overflow用户

发布于 2015-07-08 04:26:05

你应该

代码语言:javascript
复制
try
        {
            SftpATTRS atributes = this.SftpChannel.stat(path);
            return atributes.isDir();


        }

但对我来说,这是不起作用的,我得到了一个像目录这样的文件

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28646432

复制
相关文章

相似问题

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