zoukankan      html  css  js  c++  java
  • Ftp协议

    本文引用于:http://www.codeproject.com/KB/IP/ftp.aspx

    Introduction

    For my first experience writing a C# component I decided to implement an FTP Component. This is the sample code to use the component. The component code is not really guaranteed to work fine in this state, but I thought that it might be of some interest and that feedback will help to improve or correct features.

    Simply add the component to the ToolBox (Using customize Toolbox) and put it on your form. The code project contains a simple FTP Client. You may have to change FTPCom Reference in TestFTPCom project to test the sample. Remove the old reference and Add Reference to FtpCom.DLL

    Sample Code

    Connect to the FTP Server

    ftpc.Username = EFUsername.Text;
    ftpc.Password = EFPassword.Text;
    
    ftpc.Hostname = CBFTPServer.Text;
    ftpc.Connect();
    

    Logging in to the server

    When the connection is completed, the object receives the event Connected, you can then send the Login Command.

    private void ftpc_Connected(object sender, FTPCom.FTPEventArgs e)
    {
        ftpc.Login();
    }
    

    The event Logged is sent after successful connection

    private void ftpc_Logged(object sender, FTPCom.FTPEventArgs e)
    {
        ftpc.Dir();
    }
     

    Getting a directory listing

    The DirCompleted event is received when Dir command is completed FileCount contains the number of files, use IsFolder to find if File is a folder. GetFileName and GetFileSize return the name and the size of the files

    Note : File Collection is not implemented in this version as it should be!

    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;
    }
    

    Downloading a file

    File Download on Local View drag drop

    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);
        }
    }
    

    When downloading a file is complete the FileDownloadCompleted event is fired

    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);
    }
    

    Deleting selected files

    for (int i = 0; i < ServerView.SelectedItems.Count; i++)
    {
        ftpc.Delete (ServerView.SelectedItems[i].Text);
    }
    ftpc.Dir();
    

    Rename a file

    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();
        }
    }
    

    Close the connection

    ftpc.Disconnect();
    ServerView.Items.Clear();
    
  • 相关阅读:
    接口测试入门(5)----新框架重构,使用轻量级的HTTP开发库 Unirest
    接口测试入门(4)--接口自动化测试框架 / list和map用法 / 随机选取新闻 (随机数生成) / 接口相关id映射
    接口测试入门(3)--使用httpClient进行登录用例操作/set-cookies验证/ List<NameValuePair>设置post参数/json解析
    接口测试入门(2)--get和post初级请求/使用httpclient做一个获取信息list的请求(需要登录才可以)
    接口测试学习入门(1)--前期知识储备
    j2ee 使用db.properties连接mysql数据库
    com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别 serverTimezone设定
    mysql JDBC URL格式各个参数详解
    mysql新建数据库时的collation选择(转)
    SpringBoot MyBatis 配置多数据源 (静态多个)
  • 原文地址:https://www.cnblogs.com/hankskfc/p/2268044.html
Copyright © 2011-2022 走看看