Click here to Skip to main content
13,357,363 members (70,991 online)
Click here to Skip to main content
Add your own
alternative version


102 bookmarked
Posted 29 May 2002

FTP Component in C#, for .NET

, 29 May 2002
Rate this:
Please Sign up or sign in to vote.
An FTP component and sample FTP client, in C#.


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;

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)

The event Logged is sent after successful connection

private void ftpc_Logged(object sender, FTPCom.FTPEventArgs e)

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;


    ListViewItem lvItem = new ListViewItem("..");

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

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;

Deleting selected files

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

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.Rename(m_previousfilename, newfilename);

Close the connection


Hope this help. Any comments are welcome.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

GeneralFTP Commponent Pin
cndy1985010620-Apr-09 0:41
membercndy1985010620-Apr-09 0:41 
GeneralRe: FTP Commponent Pin
cwvdavid26-Sep-09 9:38
membercwvdavid26-Sep-09 9:38 
GeneralCool Pin
Axonn Echysttas12-Mar-09 14:50
memberAxonn Echysttas12-Mar-09 14:50 
GeneralA comfortable FTP class in .NET Pin
Elmue27-Aug-08 13:18
memberElmue27-Aug-08 13:18 
Questionftp folder download Pin
Member 471155710-Apr-08 0:13
memberMember 471155710-Apr-08 0:13 
GeneralNot listing the remote directory contents completely Pin
MathachanJK28-Nov-06 20:10
memberMathachanJK28-Nov-06 20:10 
QuestionRe: Not listing the remote directory contents completely Pin
Member 471155710-Apr-08 0:30
memberMember 471155710-Apr-08 0:30 
QuestionDownloading FTP Folder [modified] Pin
shafqatmasood9-Aug-06 20:37
membershafqatmasood9-Aug-06 20:37 
AnswerRe: Downloading FTP Folder Pin
shafqatmasood10-Aug-06 23:19
membershafqatmasood10-Aug-06 23:19 
QuestionRe: Downloading FTP Folder Pin
Member 47115579-Apr-08 23:56
memberMember 47115579-Apr-08 23:56 
raznalove1-Jul-06 3:37
memberraznalove1-Jul-06 3:37 
GeneralCode does not work Pin
mbde23-Mar-06 3:32
membermbde23-Mar-06 3:32 
GeneralFTP events in c# Pin
CRASH726-Dec-05 1:04
memberCRASH726-Dec-05 1:04 
Generalnot all data Pin
drxy13-Feb-05 21:30
memberdrxy13-Feb-05 21:30 
GeneralJaimon's FTP Client library may help Pin
Casp14-Dec-04 6:54
memberCasp14-Dec-04 6:54 
GeneralNon-Compatibility bug with Sun Pin
Anonymous24-Nov-04 4:09
sussAnonymous24-Nov-04 4:09 
GeneralRe: Non-Compatibility bug with Sun Pin
mrBussy1-Aug-05 5:44
membermrBussy1-Aug-05 5:44 
GeneralAnother free, open-source .NET FTP component Pin
Hans Andersen10-Nov-04 13:15
sussHans Andersen10-Nov-04 13:15 
GeneralRe: Another free, open-source .NET FTP component Pin
zini3-Mar-06 11:43
memberzini3-Mar-06 11:43 
GeneralRe: Another free, open-source .NET FTP component [modified] Pin
h_c_a_andersen12-Dec-06 17:45
memberh_c_a_andersen12-Dec-06 17:45 
GeneralWrong in the GetIcon(string strPath, bool bSmall) method Pin
ande_vega20-Oct-04 13:01
memberande_vega20-Oct-04 13:01 
GeneralTwo question Pin
CliniC16-Aug-04 3:19
memberCliniC16-Aug-04 3:19 
GeneralInput fail -&gt; freeze Pin
CliniC5-Jun-04 23:49
memberCliniC5-Jun-04 23:49 
First sorry for my englisch Unsure | :~

Have use this component, but when the server not exist, the programm will be freeze. How can I fix this bug?

QuestionHow to use it in background? Pin
Jackfan23-Mar-04 22:45
memberJackfan23-Mar-04 22:45 
AnswerRe: How to use it in background? Pin
byshome15-Sep-05 21:30
memberbyshome15-Sep-05 21:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 30 May 2002
Article Copyright 2002 by Jerome Lacaille
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid