Click here to Skip to main content
Click here to Skip to main content

LightFTPClient: a basic FTP client

, 9 May 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
LightFTPClient is a basic FTP client.

Introduction

This is a simple FTP client written in C#.

Usage

LightFTPClient is a basic FTP client. To implement FTP communication, I adapted the FTP client library of Jaimon Mathew.

LightFTPClient maintains data (username, password, port) of your connections in a list, so you can select one directly from the list. These data will be persisted into a file named lightftpclient.bin. This file will be created in the same directory where the executable is located. Data is saved when the program is closed.

Sample screenshot

System and error messages are shown in the middle area. At the bottom, on the left, there is the local path and its contents, on the right there is the remote path and its contents.

The following function is invoked to populate list referring local path.

protected void PopulateLocalFileList()
{
    //Populate listview with files
    string[] lvData =  new string[4];
    string sPath = txtLocalPath.Text;

    InitListView(lvLocalFiles);

    if (sPath.Length > 3)
        addParentDirectory(lvLocalFiles);
    else
    {

    }
    try
    {
        string[] stringDir = Directory.GetDirectories(sPath);
        string[] stringFiles = Directory.GetFiles(sPath);

        string stringFileName = "";
        DateTime dtCreateDate, dtModifyDate;
        Int64 lFileSize = 0;

        foreach (string stringFile in stringDir)
        {
            stringFileName = stringFile;
            FileInfo objFileSize = new FileInfo(stringFileName);
            lFileSize = 0;
            dtCreateDate = objFileSize.CreationTime;
                        dtModifyDate = objFileSize.LastWriteTime; 

            //create listview data
            lvData[0] = "";
            lvData[1] = GetPathName(stringFileName);
            lvData[2] = formatSize(lFileSize);
            lvData[3] = formatDate(dtModifyDate);

            //Create actual list item
            ListViewItem lvItem = new ListViewItem(lvData,0); // 0 = directory
            lvLocalFiles.Items.Add(lvItem);

        }

        foreach (string stringFile in stringFiles)
        {
            stringFileName = stringFile;
            FileInfo objFileSize = new FileInfo(stringFileName);
            lFileSize = objFileSize.Length;
            dtCreateDate = objFileSize.CreationTime; 
                        dtModifyDate = objFileSize.LastWriteTime; 

            //create listview data
            lvData[0] = "";
            lvData[1] = GetPathName(stringFileName);
            lvData[2] = formatSize(lFileSize);
            lvData[3] = formatDate(dtModifyDate);

            //Create actual list item
            ListViewItem lvItem = new ListViewItem(lvData,1); // 1 = file
            lvLocalFiles.Items.Add(lvItem);

        }

        // Loop through and size each column header
        // to fit the column header text.
        foreach (ColumnHeader ch in this.lvLocalFiles.Columns)
        {
            ch.Width = -2;
        }

    }
    catch (IOException)
    {
        MessageBox.Show("Error: Drive not ready or directory does not exist.");
    }
    catch (UnauthorizedAccessException)
    {
        MessageBox.Show("Error: Drive or directory access denided.");
    }
    catch (Exception ee)
    {
        MessageBox.Show("Error: " + ee);
    }
    lvLocalFiles.Invalidate();
    lvLocalFiles.Update();

}

Clicking on the header "Name", you can sort file list. To sort the file list, I make an implementation of the IComparer interface.

public class ListViewColumnSorter : IComparer
{
    /// <SUMMARY>
    /// Specifies the column to be sorted
    /// </SUMMARY>
    private int ColumnToSort;
    /// <SUMMARY>
    /// Specifies the order in which to sort (i.e. 'Ascending').
    /// </SUMMARY>
    private SortOrder OrderOfSort;
    /// <SUMMARY>
    /// Case insensitive comparer object
    /// </SUMMARY>
    private CaseInsensitiveComparer ObjectCompare;

    /// <SUMMARY>
    /// Class constructor.  Initializes various elements
    /// </SUMMARY>
    public ListViewColumnSorter()
    {
        // Initialize the column to '1'
        ColumnToSort = 1;

        // Initialize the sort order to 'Ascending'
        OrderOfSort = SortOrder.Ascending;

        // Initialize the CaseInsensitiveComparer object
        ObjectCompare = new CaseInsensitiveComparer();
    }

    /// <SUMMARY>
    /// This method is inherited from the IComparer interface.  
    /// It compares the two objects passed using
    /// a case insensitive comparison.
    /// 
    /// Comparison rules: 
    /// - directories are listed before (ascending)
    ///           or after (descending) than files;
    /// - both directories both files are listed
    ///           in ascending or descending order.
    /// </SUMMARY>
    /// <PARAM name="x">First object to be compared</PARAM>
    /// <PARAM name="y">Second object to be compared</PARAM>
    /// <RETURNS>The result of the comparison. "0" if equal,
    /// negative if 'x' is less than 'y' and positive
    /// if 'x' is greater than 'y'</RETURNS>
    public int Compare(object x, object y)
    {
        int compareResult;
        ListViewItem listviewX, listviewY;

        // Cast the objects to be compared to ListViewItem objects
        listviewX = (ListViewItem)x;
        listviewY = (ListViewItem)y;

        if ( (listviewX.ImageIndex == listviewY.ImageIndex) 
          ||  ((listviewX.ImageIndex +listviewY.ImageIndex) == 2) )
        {
            // items are of the same type (2 directory or 2 files)
            // with a tirck (listviewX.ImageIndex +listviewY.ImageIndex)
            // one is a link, one is a dir
            // Compare the two items
            compareResult = 
              ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text,
              listviewY.SubItems[ColumnToSort].Text);
        }
        else
        {
            // items are of different type
            // directory are listed before (ascending order) than files
            // or after (descending order) than files
            // Note: in my FTP client
            // ImageIndex = 0 means that item is a directory
            // ImageIndex = 1 means that item is a file
            // ImageIndex = 2 means that item is a link
            compareResult = 
              ((listviewX.ImageIndex < listviewY.ImageIndex)?-1:1);
        }

        // Calculate correct return value based on object comparison
        if (OrderOfSort == SortOrder.Ascending)
        {
            // Ascending sort is selected, return normal
            // result of compare operation
            return compareResult;
        }
        else if (OrderOfSort == SortOrder.Descending)
        {
            // Descending sort is selected,
            // return negative result of compare operation
            return (-compareResult);
        }
        else
        {
            // Return '0' to indicate they are equal
            return 0;
        }
    }
    
    /// <SUMMARY>
    /// Gets or sets the number of the column to which
    /// to apply the sorting operation (Defaults to '0').
    /// </SUMMARY>
    public int SortColumn
    {
        set
        {
            ColumnToSort = value;
        }
        get
        {
            return ColumnToSort;
        }
    }

    /// <SUMMARY>
    /// Gets or sets the order of sorting to apply
    /// (for example, 'Ascending' or 'Descending').
    /// </SUMMARY>
    public SortOrder Order
    {
        set
        {
            OrderOfSort = value;
        }
        get
        {
            return OrderOfSort;
        }
    }
}

And I assigned it to the ListView.

.....
// Create an instance of a ListView column sorter and assign it 
// to ListView control.
lvwColumnSorter = new ListViewColumnSorter();
this.lvFiles.ListViewItemSorter = lvwColumnSorter;

lvwLocalColumnSorter = new ListViewColumnSorter();
this.lvLocalFiles.ListViewItemSorter = lvwLocalColumnSorter;
.....

To navigate, use mouse or type path in the respective input box, then type "Enter".

Use drag & drop to download or upload a file, and the context menu to rename/delete it or to refresh content (it's also possible to use function key).

Sample screenshot

System Requirements

This application was developed using VS.NET/C# and Microsoft .NET Framework 1.1, and tested on Win 2000.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Massimo Beatini
Web Developer
Italy Italy
No Biography provided

Comments and Discussions

 
QuestionHow to change the property of directory? Pinmemberwen22920218-Oct-07 18:46 
GeneralVery Nice! Pinmemberstevo346312-Sep-07 18:13 
QuestionDoes this support proxy? PinmemberSunil Kumar Sharma "Sun"27-Aug-06 22:28 
GeneralPASV and PORT Pinmemberdongshan23-Aug-06 23:04 
AnswerRe: PASV and PORT [modified] PinmemberMassimo Beatini1-Sep-06 5:53 
GeneralRe: PASV and PORT Pinmemberdongshan9-Oct-06 22:03 
GeneralRe: PASV and PORT Pinmembermaximus.dec.meridius2-Sep-09 10:47 
GeneralTimeouts in FtpClient.readLine() PinmemberSimon Steed (MadSi)4-Apr-06 4:51 
AnswerRe: Timeouts in FtpClient.readLine() PinmemberMassimo Beatini4-Apr-06 5:39 
GeneralRe: Timeouts in FtpClient.readLine() PinmemberSimon Steed (MadSi)4-Apr-06 23:46 
AnswerRe: Timeouts in FtpClient.readLine() PinmemberMassimo Beatini5-Apr-06 1:25 
GeneralRe: Timeouts in FtpClient.readLine() Pinmemberregistrationisformexicans21-Dec-06 8:08 
QuestionAdditions? Pinmemberrball15-Mar-06 14:06 
AnswerRe: Additions? PinmemberMassimo Beatini16-Mar-06 0:17 
GeneralRe: Additions? Pinmemberrball16-Mar-06 7:06 
GeneralError return : invalid response for PASV command Pinmemberseb.4928-Feb-06 5:33 
AnswerRe: Error return : invalid response for PASV command PinmemberMassimo Beatini28-Feb-06 21:55 
GeneralRe: Error return : invalid response for PASV command Pinmemberseb.491-Mar-06 0:01 
AnswerRe: Error return : invalid response for PASV command PinmemberMassimo Beatini1-Mar-06 0:34 
GeneralRe: Error return : invalid response for PASV command Pinmemberseb.491-Mar-06 3:26 
AnswerRe: Error return : invalid response for PASV command PinmemberMassimo Beatini1-Mar-06 3:49 
GeneralRe: Error return : invalid response for PASV command Pinmemberseb.491-Mar-06 4:19 
AnswerRe: Error return : invalid response for PASV command PinmemberMassimo Beatini1-Mar-06 4:25 
GeneralRe: Error return : invalid response for PASV command Pinmemberseb.491-Mar-06 4:37 
AnswerRe: Error return : invalid response for PASV command PinmemberMassimo Beatini1-Mar-06 5:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 10 May 2004
Article Copyright 2004 by Massimo Beatini
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid