LightFTPClient: a basic FTP client






4.39/5 (9 votes)
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.
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).
System Requirements
This application was developed using VS.NET/C# and Microsoft .NET Framework 1.1, and tested on Win 2000.