Click here to Skip to main content
Click here to Skip to main content

C# FTP Client Library

, 23 May 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Easy to use FTP client library with features in mind.

Introduction

This code is an FTP class library for adding FTP access to your projects. It is heavily based on a project (see background for original code), and is now contained in the source files. I was not sure how to update the original article or add to it so I am publishing this as a new article since there was an extensive re-work of the original code in an attempt to make it more developer-friendly.

Background

This article, and code, are based upon the ftplib posted by J.P. Trosclair. A link to the article is here. I have left original copyright and credit notices in with the updated version of the code.

Using the code

I used nDoc to create a CHM file (in the doc directory of the code) to help with implementation. Its fairly simple, and you will mainly need basic FTP connection information (IP, username, etc.) to use the library.

An example is included in the documentation, and appears as so (to upload a file):

OpenFTP.FTP f = new OpenFTP.FTP();
f.Connect("127.0.0.1", "username_here", "password_here");
f.ChangeDirectory("somedirectory");
f.Files.Upload(Path.GetFileName(sFileName), Path.GetFileName(sFileName));

while (!f.Files.UploadComplete)
{
    Console.WriteLine("Uploading: TotalBytes: " + 
        f.Files.TotalBytes.ToString() + ", : PercentComplete: " + 
        f.Files.PercentComplete.ToString());
}
Console.WriteLine("Upload Complete: TotalBytes: " + 
    f.Files.TotalBytes.ToString() + ", : PercentComplete: " + 
    f.Files.PercentComplete.ToString());

f.Disconnect();
f = null;

Points of Interest

I liked the original article but decided that even though I could read and implement the code I wanted its implementation to be more simplistic so months later I won't have to re-read any code to figure how to include the methods in a new project. I have tried to remove the rough edges.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

jmers
Web Developer
Anonymous Proxy Anonymous Proxy
I am a Senior-level .NET developer using C# for about 5+ years now.
 
Codeproject has been a tremendous resource for me over the years.

Comments and Discussions

 
QuestionThank you PinmemberJason asdf29-Apr-14 11:52 
QuestionHow to check if the connection is established? PinmemberAnLa06109527-Jun-11 4:08 
GeneralAlex FTPS Client PinmemberB.Ritter5-Mar-11 0:09 
Generalexpect a demo Pinmemberpclion13-Feb-11 22:55 
GeneralNot work in download .. PinmemberAlexB4712-Feb-11 7:33 
GeneralBug solutions Pinmemberrobnet17-Aug-10 0:28 
GeneralRe: Bug solutions PinmemberB.Ritter1-Nov-10 6:37 
My solution to this problem:
 
private static Match GetMatchingRegex(string line)
{
    Regex rx;
    Match m;
    for (int i = 0; i <= _ParseFormats.Length - 1; i++)
    {
        rx = new Regex(_ParseFormats[i]);
        m = rx.Match(line);
        if (m.Success)
        {
            return m;
        }
    }
    return null;
}
 
#region "Regular expressions for parsing LIST results"
        
/// <summary>
/// List of REGEX formats for different FTP server listing formats
/// </summary>
/// <remarks>
/// The first three are various UNIX/LINUX formats, fourth is for MS FTP
/// in detailed mode and the last for MS FTP in 'DOS' mode.
/// </remarks>
private static string[] _ParseFormats = new string[] { 
    "(?<dir>[\\-d])(?<permission>([\\-r][\\-w][\\-xs]){3})\\s+\\d+\\s+\\w+\\s+\\w+\\s+(?<size>\\d+)\\s+(?<timestamp>\\w+\\s+\\d+\\s+\\d{4})\\s+(?<name>.+)", 
    "(?<dir>[\\-d])(?<permission>([\\-r][\\-w][\\-xs]){3})\\s+\\d+\\s+\\d+\\s+(?<size>\\d+)\\s+(?<timestamp>\\w+\\s+\\d+\\s+\\d{4})\\s+(?<name>.+)", 
    "(?<dir>[\\-d])(?<permission>([\\-r][\\-w][\\-xs]){3})\\s+\\d+\\s+\\d+\\s+(?<size>\\d+)\\s+(?<timestamp>\\w+\\s+\\d+\\s+\\d{1,2}:\\d{2})\\s+(?<name>.+)", 
    "(?<dir>[\\-d])(?<permission>([\\-r][\\-w][\\-xs]){3})\\s+\\d+\\s+\\w+\\s+\\w+\\s+(?<size>\\d+)\\s+(?<timestamp>\\w+\\s+\\d+\\s+\\d{1,2}:\\d{2})\\s+(?<name>.+)", 
    "(?<dir>[\\-d])(?<permission>([\\-r][\\-w][\\-xs]){3})(\\s+)(?<size>(\\d+))(\\s+)(?<ctbit>(\\w+\\s\\w+))(\\s+)(?<size2>(\\d+))\\s+(?<timestamp>\\w+\\s+\\d+\\s+\\d{2}:\\d{2})\\s+(?<name>.+)", 
    "(?<timestamp>\\d{2}\\-\\d{2}\\-\\d{2}\\s+\\d{2}:\\d{2}[Aa|Pp][mM])\\s+(?<dir>\\<\\w+\\>){0,1}(?<size>\\d+){0,1}\\s+(?<name>.+)",
    "(?<dir>[\\-d])(?<permission>([\\-r][\\-w][\\-xs]){3})\\s+\\d+\\s+\\(\\w+\\)\\s+\\(\\w+\\)\\s+(?<size>\\d+)\\s+(?<timestamp>\\w+\\s+\\d+\\s+\\d{1,2}:\\d{2})\\s+(?<name>.+)"};
 
#endregion
 
public void Add(string sUnparsedDirectory)
{
    sUnparsedDirectory = sUnparsedDirectory.Replace("\r", string.Empty);
 
    string sDirectoryName = string.Empty;
    DateTime dtDirectoryDate;
 
    //parse line
    Match m = GetMatchingRegex(sUnparsedDirectory);
    if (m == null)
    {
        //failed
        throw (new ApplicationException("Unable to parse line: " + sUnparsedDirectory));
    }
    else
    {
        sDirectoryName = m.Groups["name"].Value;
 
        if (DateTime.TryParse(m.Groups["timestamp"].Value, out dtDirectoryDate) == false)
        {
            dtDirectoryDate = DateTime.MinValue;
        }
    }
 
    Add(sDirectoryName, dtDirectoryDate);
}
 
public void Add(string sUnparsedFile)
{
    sUnparsedFile = sUnparsedFile.Replace("\r", string.Empty);
 
    string sFileName = string.Empty;
    DateTime dtFileDate;
    long lFileSize = 0;
 
    //parse line
    Match m = GetMatchingRegex(sUnparsedFile);
    if (m == null)
    {
        //failed
        throw (new ApplicationException("Unable to parse line: " + sUnparsedFile));
    }
    else
    {
        sFileName = m.Groups["name"].Value;
        string sFileSize = m.Groups["size"].Value;
        long.TryParse(sFileSize, out lFileSize);
 
        if (DateTime.TryParse(m.Groups["timestamp"].Value, out dtFileDate) == false)
        {
            dtFileDate = DateTime.MinValue;
        }
    }
 
	Add(sFileName, lFileSize, dtFileDate);
}
 
Works pretty good so far Smile | :)
QuestionI cannot get the listing of the files plus other bugs PinmemberEvandrojr2-Mar-10 10:22 
GeneralActive Mode Pinmemberliglio27-Jan-10 14:33 
GeneralRe: Active Mode Pinmemberjmers27-Jan-10 17:13 
GeneralRe: Active Mode Pinmemberliglio28-Jan-10 3:13 
GeneralRe: Active Mode PinmemberDaniel Castenholz29-Jan-10 8:22 
QuestionSocketException Error Code 11004 PinmemberZac Greve10-Jan-10 10:54 
AnswerRe: SocketException Error Code 11004 Pinmemberjmers10-Jan-10 15:18 
Questionupload folder PinmemberDirekt200718-Nov-09 4:07 
GeneralCF 3.5 PinmemberGateone7512-Aug-09 2:32 
RantLOL PinmemberSeishin#8-Apr-09 2:01 
GeneralRe: LOL PinmemberPhylum12322-Oct-09 5:25 
GeneralExceoption when trying to upload [modified] PinmvpJohn Simmons / outlaw programmer8-Jan-09 11:52 
QuestionHow to connect to two FTP Servers PinmemberLearnNetnAll15-Dec-08 21:59 
GeneralA socket operation encountered a dead network Pinmemberjosephx2k715-Dec-08 12:41 
Generalgood library Pinmemberemilioarp12-Nov-08 7:30 
Generalconnection was forcibly closed by the remote host at System.Net.Sockets. Pinmembernext100years11-Nov-08 0:55 
GeneralA comfortable FTP class in .NET PinmemberElmue27-Aug-08 12:22 
GeneralChangeDirectory Chrash PinmemberMember 42541521-May-08 11:52 
GeneralRe: ChangeDirectory Chrash Pinmemberjmers3-May-08 9:36 
GeneralRe: ChangeDirectory Chrash Pinmemberstreit31-May-08 13:13 
GeneralRe: ChangeDirectory Chrash Pinmemberemilioarp2-Oct-08 8:47 
GeneralRe: ChangeDirectory Chrash PinmemberB.Ritter3-Nov-08 9:50 
GeneralRe: ChangeDirectory Chrash [modified] PinmemberB.Ritter8-Nov-08 2:42 
GeneralRe: ChangeDirectory Chrash Pinmemberemilioarp10-Nov-08 8:18 
GeneralRe: ChangeDirectory Chrash PinmemberB.Ritter10-Nov-08 22:12 
GeneralRe: ChangeDirectory Chrash Pinmemberemilioarp12-Nov-08 4:24 
GeneralRe: ChangeDirectory Chrash Pinmemberjose.mira27-Apr-09 7:38 
GeneralRe: ChangeDirectory Chrash Pinmemberjose.mira4-May-09 7:01 
Generalerror connecting the ftpserver Pinmembernanio31-Jan-08 20:56 
GeneralRe: error connecting the ftpserver Pinmemberjmers30-Apr-08 5:57 
GeneralSubdirectories when uploading Pinmemberdannywooly21-Nov-07 0:50 
GeneralRe: Subdirectories when uploading Pinmemberjmers21-Nov-07 4:24 
GeneralFiles download Pinmemberblablablaidan24-Oct-07 22:05 
GeneralRe: Files download Pinmemberjmers25-Oct-07 1:13 
GeneralRe: Files download Pinmemberblablablaidan25-Oct-07 3:09 
GeneralRe: Files download Pinmemberjmers25-Oct-07 3:43 
GeneralRe: Files download Pinmemberblablablaidan25-Oct-07 4:15 
GeneralRe: Files download Pinmemberjmers30-Apr-08 6:02 
GeneralRe: Files download PinmemberG_Pow18-Sep-08 11:39 
GeneralRe: Files download Pinmemberemilioarp12-Nov-08 7:23 
GeneralRe: Files download PinmemberMember 45941477-Apr-09 15:40 
GeneralList Directories Pinmemberblablablaidan17-Oct-07 1:58 
GeneralRe: List Directories Pinmemberjmers24-Oct-07 5:38 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.1 | Last Updated 23 May 2007
Article Copyright 2007 by jmers
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid