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()
{
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;
lvData[0] = "";
lvData[1] = GetPathName(stringFileName);
lvData[2] = formatSize(lFileSize);
lvData[3] = formatDate(dtModifyDate);
ListViewItem lvItem = new ListViewItem(lvData,0);
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;
lvData[0] = "";
lvData[1] = GetPathName(stringFileName);
lvData[2] = formatSize(lFileSize);
lvData[3] = formatDate(dtModifyDate);
ListViewItem lvItem = new ListViewItem(lvData,1);
lvLocalFiles.Items.Add(lvItem);
}
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
{
private int ColumnToSort;
private SortOrder OrderOfSort;
private CaseInsensitiveComparer ObjectCompare;
public ListViewColumnSorter()
{
ColumnToSort = 1;
OrderOfSort = SortOrder.Ascending;
ObjectCompare = new CaseInsensitiveComparer();
}
public int Compare(object x, object y)
{
int compareResult;
ListViewItem listviewX, listviewY;
listviewX = (ListViewItem)x;
listviewY = (ListViewItem)y;
if ( (listviewX.ImageIndex == listviewY.ImageIndex)
|| ((listviewX.ImageIndex +listviewY.ImageIndex) == 2) )
{
compareResult =
ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text,
listviewY.SubItems[ColumnToSort].Text);
}
else
{
compareResult =
((listviewX.ImageIndex < listviewY.ImageIndex)?-1:1);
}
if (OrderOfSort == SortOrder.Ascending)
{
return compareResult;
}
else if (OrderOfSort == SortOrder.Descending)
{
return (-compareResult);
}
else
{
return 0;
}
}
public int SortColumn
{
set
{
ColumnToSort = value;
}
get
{
return ColumnToSort;
}
}
public SortOrder Order
{
set
{
OrderOfSort = value;
}
get
{
return OrderOfSort;
}
}
}
And I assigned it to the ListView
.
.....
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.