程序說明:
原作者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();
希望給你一定的幫助。