Exemple #1
0
        private static FTPItem gernerateRootItem()
        {
            FTPItem rootFTPItem = new FTPItem();

            rootFTPItem.name = @"\";
            rootFTPItem.node = new Int32();
            rootFTPItem.size = new Int32();

            rootFTPItem.user  = String.Empty;
            rootFTPItem.group = String.Empty;

            rootFTPItem.itemPrivileges = new ItemPrivileges();
            rootFTPItem.isDirectory    = true;

            rootFTPItem.dateModified = DateTime.Now;

            return(rootFTPItem);
        }
Exemple #2
0
        public List <FTPItem> GetItemsExtended()
        {
            lockTcpClient();

            TcpListener   listner       = null;
            TcpClient     client        = null;
            NetworkStream networkStream = null;

            List <FTPItem>            items = new List <FTPItem>();
            List <FTPResponseMessage> tmpFilesAndFolderList = new List <FTPResponseMessage>();

            List <FTPResponseMessage> responseMessages;


            setTransferType(FTP.TransferModeType.ASCII);

            if (this.ConnectionMode == FTP.ConnectionMode.ACTIVE)
            {
                listner = createDataListner();
                listner.Start();
            }
            else
            {
                client = createDataClient();
            }


            responseMessages = this.sendCommand(FTPCommand.LIST);

            if (!(responseMessages[0].ResponseCode == FTPServerResponseCode.OpenedNewConnection || responseMessages[0].ResponseCode == FTPServerResponseCode.DataConnectionAlreadyOpen))
            {
                throw new Exception(responseMessages[0].Message);
            }

            if (this.ConnectionMode == FTP.ConnectionMode.ACTIVE)
            {
                client = listner.AcceptTcpClient();
            }

            networkStream = client.GetStream();

            tmpFilesAndFolderList = readLines(networkStream);

            String errorMessage = String.Empty;

            if (responseMessages.Count == 1)
            {
                responseMessages = this.readResponse();
                errorMessage     = responseMessages[0].Message;
            }

            else
            {
                errorMessage = responseMessages[1].Message;
            }

            if (!(responseMessages[0].ResponseCode == FTPServerResponseCode.DataConnectionClosing || responseMessages[1].ResponseCode == FTPServerResponseCode.DataConnectionClosing))
            {
                throw new Exception(errorMessage);
            }

            try
            {
                networkStream.Close();
                client.Close();
            }
            catch (Exception)
            {
                throw new Exception("Network Error");
            }


            if (this.ConnectionMode == FTP.ConnectionMode.ACTIVE)
            {
                listner.Stop();
            }

            unlockTcpClient();

            foreach (FTPResponseMessage item in tmpFilesAndFolderList)
            {
                items.Add(FTPItem.GetFTPItem(item.Message, (Char)32));
            }

            return(items);
        }
Exemple #3
0
        private static FTPItem gernerateRootItem()
        {
            FTPItem rootFTPItem = new FTPItem();

            rootFTPItem.name = @"\";
            rootFTPItem.node = new Int32();
            rootFTPItem.size = new Int32();

            rootFTPItem.user  = String.Empty;
            rootFTPItem.group = String.Empty;

            rootFTPItem.itemPrivileges = new ItemPrivileges();
            rootFTPItem.isDirectory    = true;

            rootFTPItem.dateModified = DateTime.Now;

            return rootFTPItem;
        }
Exemple #4
0
        public static FTPItem GetFTPItem(String _item, params Char[] _delimiter)
        {
            List <String> attributes = new List <String>();
            FTPItem       ftpItem    = new FTPItem();

            // split items
            foreach (String attribute in _item.Split(_delimiter))
            {
                if (attribute.Length != 0)
                {
                    attributes.Add(attribute);
                }
            }

            DateTime dateTime;
            String   dateTimeString = String.Empty;

            // if the ftpItem starts with modified Date ( Microsoft IIS Format )
            if (attributes[0].Contains('-') && !(attributes[0].Contains('r') || attributes[0].Contains('w') || attributes[0].Contains('x')))
            {
                try
                {
                    List <String> dateTimeParts = attributes[0].Insert(attributes[0].LastIndexOf('-') + 1, "20").Split('-').ToList();

                    dateTimeString = String.Format("{0}.{1}.{2}", dateTimeParts[1], dateTimeParts[0], dateTimeParts[2]);
                }
                catch {};
            }

            if (DateTime.TryParse(dateTimeString, out dateTime))
            {
                Int32    size;
                String   datetime = String.Format("{0} {1}", dateTimeString, attributes[1]);
                DateTime dateModified;


                if (DateTime.TryParse(datetime, out dateModified))
                {
                    ftpItem.dateModified = dateModified;
                }

                if (attributes[2].Equals("<DIR>"))
                {
                    ftpItem.isDirectory = true;

                    ftpItem.name = attributes[3];
                }

                else
                {
                    if (Int32.TryParse(attributes[2], out size))
                    {
                        ftpItem.size = size;
                    }

                    ftpItem.name = attributes[3];
                }
            }

            else
            {
                if (_item[0] == 'd' || _item[0] == 'l')
                {
                    ftpItem.isDirectory = true;
                }

                else
                {
                    ftpItem.isDirectory = false;
                }

                _item = _item.Remove(0, 1);

                String dateTimeFormatString = String.Empty;

                if (attributes[(Int32)Attribute.DateModified_TimeOrYear].Contains(':'))
                {
                    dateTimeFormatString = "{0}.{1}.2011 {2}:00";
                }

                else
                {
                    dateTimeFormatString = "{0}.{1}.{2}";
                }


                String dateModified = String.Format(dateTimeFormatString,
                                                    attributes[(Int32)Attribute.DateModified_Day],
                                                    attributes[(Int32)Attribute.DateModified_Month],
                                                    attributes[(Int32)Attribute.DateModified_TimeOrYear]
                                                    );

                ftpItem.itemPrivileges = ItemPrivileges.GetItemPrivileges(attributes[(Int32)Attribute.ItemPrivileges]);
                ftpItem.node           = Int32.Parse(attributes[(Int32)Attribute.Node]);
                ftpItem.user           = attributes[(Int32)Attribute.USER_NAME];
                ftpItem.group          = attributes[(Int32)Attribute.Group];
                ftpItem.size           = Int32.Parse(attributes[(Int32)Attribute.Size]);
                ftpItem.dateModified   = DateTime.Parse(dateModified);

                ftpItem.name = attributes[(Int32)Attribute.Name];

                for (Int32 i = (Int32)Attribute.Name + 1; i < attributes.Count; i++)
                {
                    ftpItem.name = String.Format("{0} {1}", ftpItem.name, attributes[i]);
                }
            }

            return(ftpItem);
        }
Exemple #5
0
        public static FTPItem GetFTPItem(String _item, params Char[] _delimiter)
        {
            List<String> attributes = new List<String>();
            FTPItem ftpItem = new FTPItem();

            // split items
            foreach (String attribute in _item.Split(_delimiter))
            {
                if (attribute.Length != 0)
                    attributes.Add(attribute);
            }

            DateTime dateTime;
            String dateTimeString = String.Empty;

            // if the ftpItem starts with modified Date ( Microsoft IIS Format )
            if (attributes[0].Contains('-') && !(attributes[0].Contains('r') || attributes[0].Contains('w') || attributes[0].Contains('x')))
            {
                try
                {
                    List<String> dateTimeParts = attributes[0].Insert(attributes[0].LastIndexOf('-') + 1, "20").Split('-').ToList();

                    dateTimeString = String.Format("{0}.{1}.{2}", dateTimeParts[1], dateTimeParts[0], dateTimeParts[2]);
                }
                catch{};
            }

            if (DateTime.TryParse(dateTimeString, out dateTime))
            {
                Int32 size;
                String datetime = String.Format("{0} {1}", dateTimeString, attributes[1]);
                DateTime dateModified;

                if (DateTime.TryParse(datetime, out dateModified))
                {
                    ftpItem.dateModified = dateModified;
                }

                if (attributes[2].Equals("<DIR>"))
                {
                    ftpItem.isDirectory = true;

                    ftpItem.name = attributes[3];
                }

                else
                {

                    if (Int32.TryParse(attributes[2], out size))
                    {
                        ftpItem.size = size;
                    }

                    ftpItem.name = attributes[3];
                }

            }

            else
            {
                if (_item[0] == 'd' || _item[0] == 'l')
                    ftpItem.isDirectory = true;

                else
                    ftpItem.isDirectory = false;

                _item = _item.Remove(0, 1);

                String dateTimeFormatString = String.Empty;

                if (attributes[(Int32)Attribute.DateModified_TimeOrYear].Contains(':'))
                    dateTimeFormatString = "{0}.{1}.2011 {2}:00";

                else
                    dateTimeFormatString = "{0}.{1}.{2}";

                String dateModified = String.Format(dateTimeFormatString,
                                           attributes[(Int32)Attribute.DateModified_Day],
                                           attributes[(Int32)Attribute.DateModified_Month],
                                           attributes[(Int32)Attribute.DateModified_TimeOrYear]
                                           );

                ftpItem.itemPrivileges = ItemPrivileges.GetItemPrivileges(attributes[(Int32)Attribute.ItemPrivileges]);
                ftpItem.node           = Int32.Parse(attributes[(Int32)Attribute.Node]);
                ftpItem.user           = attributes[(Int32)Attribute.USER_NAME];
                ftpItem.group          = attributes[(Int32)Attribute.Group];
                ftpItem.size           = Int32.Parse(attributes[(Int32)Attribute.Size]);
                ftpItem.dateModified   = DateTime.Parse(dateModified);

                ftpItem.name = attributes[(Int32)Attribute.Name];

                for (Int32 i = (Int32)Attribute.Name + 1; i < attributes.Count; i++)
                {
                    ftpItem.name = String.Format("{0} {1}", ftpItem.name, attributes[i]);
                }
            }

            return ftpItem;
        }