Click here to Skip to main content
13,191,642 members (65,143 online)
Click here to Skip to main content
Add your own
alternative version

Stats

285.4K views
3.9K downloads
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#.

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

Hope this help. Any comments are welcome.

License

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

Share

About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralAbout command. Pin
colormex23-Jan-04 15:42
membercolormex23-Jan-04 15:42 
Generalredirect Pin
rawad 200419-Jan-04 8:24
memberrawad 200419-Jan-04 8:24 
GeneralFTP Component Pin
rawad 200412-Jan-04 3:08
memberrawad 200412-Jan-04 3:08 
GeneralDO NOT USE THIS COMPONENT Pin
Anonymous27-May-03 5:24
sussAnonymous27-May-03 5:24 
GeneralRe: DO NOT USE THIS COMPONENT Pin
drwindows19-Nov-03 23:35
memberdrwindows19-Nov-03 23:35 
GeneralRe: DO NOT USE THIS COMPONENT Pin
Jerome Lacaille20-Nov-03 3:13
memberJerome Lacaille20-Nov-03 3:13 
GeneralJerome,I want to tell you....... Pin
lanpa00718-Dec-03 21:08
memberlanpa00718-Dec-03 21:08 
GeneralRe: DO NOT USE THIS COMPONENT Pin
B!GFooT13-Aug-07 2:28
memberB!GFooT13-Aug-07 2:28 
Generalabout DirChange() Pin
weiweiwei12-Apr-03 20:22
memberweiweiwei12-Apr-03 20:22 
GeneralRe: about DirChange() Pin
scsy15-Apr-03 7:04
sussscsy15-Apr-03 7:04 
GeneralRe: about DirChange() Pin
weiweiwei15-Apr-03 7:50
memberweiweiwei15-Apr-03 7:50 
GeneralProblem with exe Pin
sorry273-Feb-03 4:22
membersorry273-Feb-03 4:22 
GeneralRe: Problem with exe Pin
Anonymous24-Feb-03 10:03
sussAnonymous24-Feb-03 10:03 
GeneralCode does not seem to work on every ftpserver Pin
normann30-Jan-03 11:43
membernormann30-Jan-03 11:43 
GeneralRe: Code does not seem to work on every ftpserver Pin
Anonymous7-Apr-03 0:36
sussAnonymous7-Apr-03 0:36 
GeneralCurrent directory Pin
ThiefMaster26-Jan-03 2:03
memberThiefMaster26-Jan-03 2:03 
GeneralRe: Current directory Pin
Tuliplanet2-Jun-03 17:51
memberTuliplanet2-Jun-03 17:51 
GeneralFileDownloadCompleted does not report filename Pin
Kelvin Foo Chuan Lyi12-Dec-02 13:47
memberKelvin Foo Chuan Lyi12-Dec-02 13:47 
GeneralFileDownloadCompleted does not report filename Pin
Anonymous12-Dec-02 13:46
sussAnonymous12-Dec-02 13:46 
GeneralFTP Size Limitation Pin
godlikemouse6-Dec-02 7:54
sussgodlikemouse6-Dec-02 7:54 
GeneralRe: FTP Size Limitation Pin
godlikemouse6-Dec-02 11:35
sussgodlikemouse6-Dec-02 11:35 
GeneralFileCount not correct Pin
hfwangfeng18-Nov-02 20:31
memberhfwangfeng18-Nov-02 20:31 
GeneralRe: FileCount not correct Pin
godlikemouse6-Dec-02 7:55
membergodlikemouse6-Dec-02 7:55 
Questionabout the delay of "PASV" command? Pin
daemonftp16-Nov-02 0:16
memberdaemonftp16-Nov-02 0:16 
GeneralFTPC vs CPU Load Pin
Anonymous30-Oct-02 22:50
sussAnonymous30-Oct-02 22:50 
GeneralFileUpload Syntax Pin
Philip Neufeld11-Sep-02 13:25
sussPhilip Neufeld11-Sep-02 13:25 
GeneralRe: FileUpload Syntax Pin
Stotti27-May-03 1:22
memberStotti27-May-03 1:22 
Generalwaiting for event Pin
Anonymous23-Jul-02 7:12
sussAnonymous23-Jul-02 7:12 
GeneralHelp on removing error checking Pin
Anonymous22-Jul-02 9:06
sussAnonymous22-Jul-02 9:06 
GeneralDir() not updating Pin
omkamal16-Jul-02 5:39
memberomkamal16-Jul-02 5:39 
GeneralRe: Dir() not updating Pin
Anonymous19-Jul-02 15:32
sussAnonymous19-Jul-02 15:32 
GeneralRe: Dir() not updating Pin
fulgeru9930-Nov-03 4:02
memberfulgeru9930-Nov-03 4:02 
GeneralFTP using ASP.NET Pin
omkamal12-Jul-02 10:14
memberomkamal12-Jul-02 10:14 
Generalreadme-bug Pin
Rüpel25-Jun-02 3:10
memberRüpel25-Jun-02 3:10 
GeneralRe: readme-bug Pin
Jerome Lacaille25-Jun-02 6:06
memberJerome Lacaille25-Jun-02 6:06 
Generaldirectories missing Pin
Rüpel24-Jun-02 3:13
memberRüpel24-Jun-02 3:13 
GeneralRe: directories missing Pin
Rüpel24-Jun-02 3:42
memberRüpel24-Jun-02 3:42 

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
Web02 | 2.8.171017.2 | Last Updated 30 May 2002
Article Copyright 2002 by Jerome Lacaille
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid