示例#1
0
文件: FTP.cs 项目: zimmerrol/FTPNet
        public List <IOElement> ListFiles()
        {
            _checkConnected();

            string answer = SendCommandReadAnswer("LIST");

            string passiveAnswer = ReadAnswer(PassiveClient, "000", false).Trim();

            //dispose passive objects
            PassiveClient.Close();
            PassiveClient = null;
            PassiveClientReader.Close();
            PassiveClientReader = null;

            if (answer.Contains("226") == false)
            {
                answer = ReadAnswer();
            }

            List <IOElement> Files = new List <IOElement>();

            if (passiveAnswer.Length == 0)
            {
                return(Files);
            }

            //detect os for file listing parsing
            int os = 0;

            string[] osHelper = passiveAnswer.Split('\n');

            if (osHelper.Length > 0)
            {
                os = (osHelper[0][10] == ' ' && osHelper[0][9] != ' ') ? 0 : 1;
            }

            Files = ((OS & (OSType)os) == OSType.Windows) ? IOElement.ParseWindowsFiles(passiveAnswer) : IOElement.ParseLinuxFiles(passiveAnswer);

            Files.Sort();
            return(Files);
        }
示例#2
0
        public async Task <List <IOElement> > ListFiles(string absoluteDirectoryName)
        {
            checkConnected();

            IsIdled = false;

            if (_dataChannelSocket == null)
            {
                if (EnterPassiveModeAutomatic)
                {
                    await EnterPassiveMode();
                }
            }

            string answer;

            if (!TrySendCommandReadAnswer("LIST " + absoluteDirectoryName, out answer))
            {
                return(null);
            }

            if (answer == null)
            {
                return(null);
            }

            string passiveAnswer = string.Empty;

            try
            {
                passiveAnswer = ReadAnswer(ClientType.PassiveClient, false, false).Trim();
            }
            catch (IOException) { throw; }

            //dispose passive objects
            _dataChannelSocket.Dispose();
            _dataChannelSocket                   = null;
            _dataChannelSocketReader             = null;
            _dataChannelSocketWriter             = null;
            _dataChannelSocketTlsProtocolHandler = null;

            IsIdled = true;

            if (answer.Contains("226") == false)
            {
                try
                {
                    answer = ReadAnswer(ClientType.ActiveClient);
                }
                catch (IOException) { throw; }
            }

            List <IOElement> files = new List <IOElement>();

            if (passiveAnswer.Length == 0)
            {
                return(files);
            }

            //detect os for file listing parsing
            int os = 0;

            string[] osHelper = passiveAnswer.Split('\n');

            if (osHelper.Length > 0)
            {
                os = (osHelper[0][10] == ' ' && osHelper[0][9] != ' ') ? 0 : 1;
            }

            //set absoluteDirectoryName to CurrentWorkingDirectory if it is not specified
            if (absoluteDirectoryName == string.Empty)
            {
                absoluteDirectoryName = CurrentWorkingDirectory;
            }

            files = ((_parentClient.OS & (OSType)os) == OSType.Windows) ? await IOElement.ParseWindowsFiles(passiveAnswer, absoluteDirectoryName) : await IOElement.ParseLinuxFiles(passiveAnswer, absoluteDirectoryName);

            files.Sort();
            return(files);
        }