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