[Post] FTP component in C # And simple FTP Client

Source: Internet
Author: User
Tags ftp client

Program description:

Original Author Jerome lacaille, code size: k48.
Environment: C #,. net
The following is a description by the author of the translation.

Introduction

At this time, I wrote a C # component for the first time. I decided to create a component to implement FTP. Here is a simple code to use this component. I cannot guarantee that the code of this component can work well under any circumstances, but I think I will improve it in the feedback I get.

Add this component to Toolbox (using custom Toolbox) and put it in your form. This project includes a simple FTP client.

Code:
Connect to the FTP server:
Ftpc. Username = efusername. text;
Ftpc. Password = efpassword. text;

Ftpc. hostname = cbftpserver. text;
Ftpc. Connect ();

Login Server:
Private void ftpc_connected (Object sender, ftpcom. ftpeventargs E)
{
Ftpc. login ();
}

After successful connection, the time will be recorded:
Private void ftpc_logged (Object sender, ftpcom. ftpeventargs E)
{
Ftpc. dir ();
}

Get a directory list:
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;
}

Download an object:
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 the download ends, a filedownloadcompleted event will be returned:
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 );
}

Delete an object:
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_previusfilename = "new folder ")
{
Ftpc. dircreate (newfilename );
}
Else
{
Ftpc. Rename (m_previusfilename, newfilename );
}
Ftpc. dir ();
}
}

Disconnect:
Ftpc. Disconnect ();
Serverview. Items. Clear ();

Hope to help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.