Get FTP File Attributes

+1 vote
asked Feb 25, 2010 by Ronak Patel (160 points)
edited Apr 4, 2011

How do i get FTP File attributes like one can see in Permissions Column in Filezilla.

Applies to: Rebex FTP/SSL

1 Answer

0 votes
answered Feb 25, 2010 by Lukas Pokorny (109,650 points)
edited Apr 4, 2011
 
Best answer

UPDATE: Parsing of Group, Owner and Permissions was added to FTP/SSL version 3.0.4060.

Before it this was not supported out-of-the-box, but you can easily add a custom parser for permissions and owner/group attributes.

The code you need:

    /// <summary>
    /// This class extends FtpItem class by unix rights mask.
    /// </summary>
    public class UnixItem : FtpItem
    {
        private int _rights;

public int Rights
        {
            get { return _rights; }
        }

public UnixItem(int rights, FtpItem item)
            : base(item.Name, item.Size, item.Type,
                  item.Modified, item.SymlinkPath)
        {
            _rights = rights;
        }

/// <summary>
        /// Custom parser that parses Unix LIST lines
        /// and creates UnixItem items.
        /// </summary>
        public static void Parser(object sender, FtpItemParseEventArgs e)
        {
            // if the item was ignored, ignore it as well
            if (e.Item == null)
                return;

// if the first space is not at position 10,
            // the item is not a Unix LIST item
            if (e.RawLine.IndexOf(' ') != 10)
                return;

// compute the rights mask
            int mask = 256;
            int rights = 0;
            for (int i=1; i<10; i++)
            {
                if (e.RawLine[i] != '-')
                    rights += mask;
                mask /= 2;
            }

// create unix item with rights
            e.Item = new UnixItem(rights, e.Item);
        }
    }

Then, instruct FtpItem to call the enhanced parser:

FtpItem.ItemParse += new FtpItemParseEventHandler(UnixItem.Parser);

(Only do that once in our application.)

This will make the FtpList instance returned by GetList method contain instances of UnixItem instead of FtpItem, making it possible to do this later:

    UnixItem unixItem = item as UnixItem;
    if (unixItem != null)
        permissions = Convert.ToString (unixItem.Rights, 8).PadLeft(3,'0') + " ";
    else
        permissions = "??? ";

To see a working implementation of this, check out the Console FTP Client sample that comes with Rebex install package.

...