// Parses a line from a Windows-format listing
        //
        // Assumes listing style as:
        // 02-03-04  07:46PM       <DIR>          Append
        protected FtpDirectoryEntry ParseWindowsDirectoryListing(string text)
        {
            FtpDirectoryEntry entry = new FtpDirectoryEntry();

            text = text.Trim();
            string dateStr = text.Substring(0, 8);

            text = text.Substring(8).Trim();
            string timeStr = text.Substring(0, 7);

            text = text.Substring(7).Trim();

            entry.CreateTime = DateTime.ParseExact(dateStr, "dd/MM/yyyy hh:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None);
            //entry.CreateTime = DateTime.Parse(String.Format("{0} {1}", dateStr, timeStr));
            if (text.Substring(0, 5) == "<DIR>")
            {
                entry.IsDirectory = true;
                text = text.Substring(5).Trim();
            }
            else
            {
                entry.IsDirectory = false;
                int pos = text.IndexOf(' ');
                entry.Size = Int64.Parse(text.Substring(0, pos));
                text       = text.Substring(pos).Trim();
            }
            entry.Name = text;  // Rest is name

            return(entry);
        }
        // Converts a directory listing to a list of FtpDirectoryEntrys
        protected List <FtpDirectoryEntry> ParseDirectoryListing(string listing)
        {
            ParseLine parseFunction          = null;
            List <FtpDirectoryEntry> entries = new List <FtpDirectoryEntry>();

            string[]           lines  = listing.Split('\n');
            FtpDirectoryFormat format = GuessDirectoryFormat(lines);

            if (format == FtpDirectoryFormat.Windows)
            {
                parseFunction = ParseWindowsDirectoryListing;
            }
            else if (format == FtpDirectoryFormat.Unix)
            {
                parseFunction = ParseUnixDirectoryListing;
            }

            if (parseFunction != null)
            {
                foreach (string line in lines)
                {
                    if (line.Length > 0)
                    {
                        FtpDirectoryEntry entry = parseFunction(line);
                        if (entry.Name != "." && entry.Name != "..")
                        {
                            // var p = entry.Name.Substring(0, entry.Name.Length - 14);
                            entry.Name = entry.Name.Substring(0, entry.Name.Length).ToString();
                        }
                        entries.Add(entry);
                    }
                }
            }
            return(entries);
        }
        // Delete Files command
        private void tsmiDelete_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection items = listView1.SelectedItems;
            if (items.Count > 0)
            {
                string description;
                if (items.Count > 1)
                {
                    description = String.Format("{0} items", items.Count);
                }
                else
                {
                    description = String.Format("'{0}'", ((FtpDirectoryEntry)items[0].Tag).Name);
                }

                if (MessageBox.Show(String.Format("Are you sure you want to permanently delete {0}?", description),
                                    "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    foreach (ListViewItem item in items)
                    {
                        FtpDirectoryEntry entry = (FtpDirectoryEntry)item.Tag;
                        if (entry.IsDirectory)
                        {
                            DeleteDirectory(entry.Name);
                        }
                        else
                        {
                            DeleteFiles(entry.Name);
                        }
                    }
                }
            }
        }
        // Parses a line from a UNIX-format listing
        //
        // Assumes listing style as:
        // dr-xr-xr-x   1 owner    group               0 Nov 25  2002 bussys
        protected FtpDirectoryEntry ParseUnixDirectoryListing(string text)
        {
            // Assuming record style as
            // dr-xr-xr-x   1 owner    group               0 Nov 25  2002 bussys
            //FtpDirectoryEntry entry = new FtpDirectoryEntry();
            //string processstr = text.Trim();
            ////string targetDelValue = processstr.Substring(0, processstr.Length - 14);
            ////processstr = targetDelValue + ".";
            //entry.Flags = processstr.Substring(0, 9);
            //entry.IsDirectory = (entry.Flags[0] == 'd');
            //processstr = (processstr.Substring(11)).Trim();
            //CutSubstringWithTrim(ref processstr, ' ', 0);   //skip one part
            //entry.Owner = CutSubstringWithTrim(ref processstr, ' ', 0);
            //entry.Group = CutSubstringWithTrim(ref processstr, ' ', 0);
            //CutSubstringWithTrim(ref processstr, ' ', 0);   //skip one part
            //entry.CreateTime = DateTime.Parse(CutSubstringWithTrim(ref processstr, ' ', 8));
            //                                                //entry.CreateTime = DateTime.Parse(CutSubstringWithTrim(ref processstr, ' ', 8));
            //                                                //  entry.Name = processstr;   //Rest of the part is name
            //entry.Name = processstr;
            //return entry;

            FtpDirectoryEntry entry = new FtpDirectoryEntry();


            if (text != null)
            {
                try
                {
                    string processstr = text.Trim();
                    //string targetDelValue = processstr.Substring(0, processstr.Length - 14);
                    //processstr = targetDelValue + ".";
                    entry.Flags       = processstr.Substring(0, 9);
                    entry.IsDirectory = (entry.Flags[0] == 'd');
                    processstr        = (processstr.Substring(11)).Trim();
                    CutSubstringWithTrim(ref processstr, ' ', 0);   //skip one part
                    entry.Owner = CutSubstringWithTrim(ref processstr, ' ', 0);
                    entry.Group = CutSubstringWithTrim(ref processstr, ' ', 0);
                    CutSubstringWithTrim(ref processstr, ' ', 0);   //skip one part
                    entry.CreateTime = DateTime.Parse(CutSubstringWithTrim(ref processstr, ' ', 8));
                    //entry.CreateTime = DateTime.Parse(CutSubstringWithTrim(ref processstr, ' ', 8));
                    //  entry.Name = processstr;   //Rest of the part is name
                    entry.Name = processstr;
                }
                catch (Exception ex)
                {
                    entry.Name = "HATALI";
                }
            }

            return(entry);
        }
        // Returns the list of selected files. Directories are not included.
        protected List <string> GetSelectedFiles()
        {
            // Build list of selected files
            List <string> files = new List <string>();

            foreach (ListViewItem item in listView1.SelectedItems)
            {
                FtpDirectoryEntry entry = (FtpDirectoryEntry)item.Tag;
                if (!entry.IsDirectory)
                {
                    files.Add(entry.Name);
                }
            }
            return(files);
        }
 // Handle Enter or mouse double click
 protected void ChooseItem()
 {
     if (AllowDirectoryNavigation)
     {
         ListView.SelectedListViewItemCollection items = listView1.SelectedItems;
         if (items.Count > 0)
         {
             FtpDirectoryEntry entry = (FtpDirectoryEntry)items[0].Tag;
             if (entry.IsDirectory)
             {
                 SetDirectory(entry.Name);
             }
         }
     }
 }
            public int Compare(object obj1, object obj2)
            {
                FtpDirectoryEntry entry1 = ((ListViewItem)obj1).Tag as FtpDirectoryEntry;
                FtpDirectoryEntry entry2 = ((ListViewItem)obj2).Tag as FtpDirectoryEntry;

                if (entry1 == null || entry2 == null)
                {
                    return(0);
                }
                else if (entry1.IsDirectory != entry2.IsDirectory)
                {
                    return((entry1.IsDirectory) ? -1 : 1);
                }
                else
                {
                    return(String.Compare(entry1.Name, entry2.Name));
                }
            }