简体   繁体   English

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

[英]How to diffenrentiate file and directory using Tamir.SharpSsh.Sftp

I am using Tamir.SharpSsh.Sftp and want to get all file name from the server and then want to show on page using dynatree. 我正在使用Tamir.SharpSsh.Sftp,想要从服务器获取所有文件名,然后使用dynatree在页面上显示。

my method is here : 我的方法在这里:

 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;
        }
    }

so please help any such type of function to detect file or folder ? 所以请帮助任何此类功能检测文件或文件夹?

You should 你应该

try
        {
            SftpATTRS atributes = this.SftpChannel.stat(path);
            return atributes.isDir();


        }

But for me, it's not working I am getting a file like a Directory 但是对我来说,它无法正常工作,我正在获取目录等文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM