程序说明:
原作者Jerome Lacaille,代码大小:k48。
环境:C#, .NET
下面是翻译的作者的说明。
简介
这时我第一次写一个C#的组件,我决定做一个实现FTP的组件。这里提供一个使用这个组件的简单的代码。我并不能保证这个组件的代码可以很好的工作于任何情况下,但是我想我会在得到的反馈中改进它。
把这个组件加入到ToolBox(使用自定义的ToolBox )中并放到你的form里。这个工程包括了一个简单的FTP客户端。
代码:
连接到FTP服务器:
ftpc.Username = EFUsername.Text;
ftpc.Password = EFPassword.Text;
ftpc.Hostname = CBFTPServer.Text;
ftpc.Connect(,',',');
登陆服务器:
private void ftpc_Connected(object sender, FTPCom.FTPEventArgs e)
{
ftpc.Login(,',',');
}
成功连接后时间将被记录:
private void ftpc_Logged(object sender, FTPCom.FTPEventArgs e)
{
ftpc.Dir(,',',');
}
得到一个目录列表:
private void ftpc_DirCompleted(object sender, FTPCom.FTPEventArgs e)
{
int i = 0;
int idimage = 0;
string msg;
msg = "Transfered " + e.TotalBytes.ToString() + " bytes in " +
((float)e.TimeElapsed / 1000).ToString() + " seconds" + CRLF;
TextLog.SelectionColor = Color.Black;
TextLog.AppendText(msg,',',');
ServerView.BeginUpdate(,',',');
ServerView.Items.Clear(,',',');
ImgListServerSmall.Images.Clear(,',',');
ListViewItem lvItem = new ListViewItem("..",',',');
ServerView.Items.Add(lvItem,',',');
for (i = 0; i < ftpc.FileCount; i++)
{
if (ftpc.IsFolder(i))
{
string[] items = new String[2];
items[0] = ftpc.GetFileName(i,',',');
items[1] = ftpc.GetFileSize(i).ToString(,',',');
ImgListServerSmall.Images.Add (m_IconFolder,',',');
ServerView.Items.Add(new ListViewItem(items, idimage++),',',');
}
}
for (i = 0; i < ftpc.FileCount; i++)
{
if (!ftpc.IsFolder(i))
{
string[] items = new String[2];
items[0] = ftpc.GetFileName(i,',',');
items[1] = ftpc.GetFileSize(i).ToString(,',',');
ImgListServerSmall.Images.Add (ExtractIcon.GetIcon(items[0], false),',',');
ServerView.Items.Add(new ListViewItem(items, idimage++),',',');
}
}
ServerView.EndUpdate(,',',');
this.Cursor = Cursors.Default;
}
下载一个文件:
private void ServerView_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (e.Button != 0)
{
string msg = "";
for (int i = 0; i < ServerView.SelectedItems.Count; i++)
{
msg += ServerView.SelectedItems[i].Text + "\n";
}
ServerView.DoDragDrop(msg, DragDropEffects.Copy | DragDropEffects.Move,',',');
}
}
private void LocalView_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void LocalView_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
string msg = e.Data.GetData(DataFormats.Text).ToString(,',',');
string[] filename = msg.Split(new char[] { '\n' },',',');
foreach (string sfile in filename)
{
ftpc.FileDownload(sfile,',',');
}
}
当下载结束时,会放回一个FileDownloadCompleted事件:
private void ftpc_FileDownloadCompleted(object sender, FTPCom.FTPEventArgs e)
{
string msg = "Transfered " + e.TotalBytes.ToString() + " bytes in " +
((float)e.TimeElapsed / 1000).ToString() + " seconds" + CRLF;
TextLog.SelectionColor = Color.Black;
TextLog.AppendText(msg,',',');
FillLocalView(m_currentFolder,',',');
}
删除文件:
for (int i = 0; i < ServerView.SelectedItems.Count; i++)
{
ftpc.Delete (ServerView.SelectedItems[i].Text,',',');
}
ftpc.Dir(,',',');
重命名文件:
private void ServerView_AfterLabelEdit(object sender,
System.Windows.Forms.LabelEditEventArgs e)
{
if (e.Label != null)
{
this.Cursor = Cursors.WaitCursor;
string newfilename = e.Label;
if (m_previousfilename == "New Folder")
{
ftpc.DirCreate(newfilename,',',');
}
else
{
ftpc.Rename(m_previousfilename, newfilename,',',');
}
ftpc.Dir(,',',');
}
}
断开连接:
ftpc.Disconnect(,',',');
ServerView.Items.Clear(,',',');
希望给你一定的帮助。