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

C# FTP client, based on the WinInet.dll API

, 16 Sep 2003
Rate this:
Please Sign up or sign in to vote.
The WinInet.dll API is able to tunnel through firewalls - this simple FTP client will support file transfer even when your firewall has only port 21open!

Introduction

When I needed to add automated file transfer to and from a remote FTP server, I found any number of excellent, clear, well structured and beautifully documented examples; both commercial and private - and all useless to me! Our Internet access is over a rather 'well throttled' firewall that seems to only have port 21 left open. The classic sockets approach to implementing an FTP client will open a 'command channel' over port 21 (default FTP port), allowing the client to sign-on etc., but as soon as any data is required to flow a second, 'data channel' must be opened (default port 20). Active or passive, I could not establish a second channel through our firewall.

I would have given up at this stage and started to look for a new employer, when it occurred to me that the Windows Explorer was perfectly able to exchange data with all and any FTP server I pointed it at... this runs over exactly the same firewall, so something strange and not just a bit magical is happening here. It turns out this bit of magic is to be found in the WinInet.dll. I am still not quite sure how the WinInet.dll works, it seems to sweet talk the firewall into opening just a small chink in its armor - but the important thing is that it works!

In order to use the FTP features of WinInet.dll, I needed to encapsulate the API in C#. This was mostly pain free, except for the marshaling of string parameters - it is documented, but you have to know what to look for. Having finished this, I figured I could offer it to the C# community; maybe I will save somebody many frustrating hours trying to bore through his corporate firewall, but also because I am quite sure I am going to get a bunch of really helpful feedback... for example, why does a successful file deletion return error code 2 = file not found? Ah, the mysteries of API programming.

I have included a very minimal console application to test the basic functionality - the default URL is ftp://ftp.microsoft.com/, users are invited to replace this with their own target.

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

About the Author

Peter Beedell
Web Developer
Germany Germany
As a Brit who sacrificed warm beer baked beans for Bayerisch lager and Bratwurst about 12 years ago, I feel European and proud of it!

Comments and Discussions

 
QuestionUnknown error code: 1008 Pinmemberdba@tom30-Aug-11 23:04 
RantInternetOpen Bug Pinmemberjbuehler25-Jan-10 15:05 
GeneralMy vote of 2 PinmvpJohn Simmons / outlaw programmer8-Jan-09 9:26 
GeneralRe: My vote of 2 PinmemberElmue11-Jan-09 5:23 
GeneralA comfortable FTP class in .NET PinmemberElmue27-Aug-08 12:22 
GeneralRe: A comfortable FTP class in .NET PinmvpJohn Simmons / outlaw programmer8-Jan-09 9:27 
GeneralRe: A comfortable FTP class in .NET PinmemberElmue11-Jan-09 5:12 
QuestionPossibility to use QUOTE RCMD SBMJOB CMD()? Pinmemberpaoloden6-Jan-07 0:58 
Generalget every file of a directory Pinmemberwilco9623-Feb-06 7:22 
AnswerRe: get every file of a directory Pinmemberantgao9-Nov-06 4:21 
GeneralRe: get every file of a directory Pinmembergan.gary15-Sep-08 23:39 
Generalimportant change required for using in .Net 2.0... PinmemberTim_Mackey8-Feb-06 2:55 
GeneralRe: important change required for using in .Net 2.0... Pinmembershareeef8-Dec-06 8:52 
GeneralRe: important change required for using in .Net 2.0... PinmemberVictortto16-Oct-07 13:08 
GeneralRe: important change required for using in .Net 2.0... PinmemberTriple-S21-Oct-07 0:52 
RantRe: important change required for using in .Net 2.0... PinmemberHAIXJ28-May-08 1:09 
GeneralBug Pinmemberelbertlev8-Jan-06 6:52 
In
public bool funGetError()
case 12003: //ERROR_INTERNET_EXTENDED_ERROR
 
call to InternetGetLastResponseInfo _OVERWRITES_ (zeroes)
the value of intErrorValue. There are 2 such calls. That's why in such cases funGetError() returnes FALSE.
 
I do not think this is by design. To fix, just add
uint intErrorValue = 0; as a first line in the try block.
 
Otherwise it as workable solution. At least can be used as starting point for ftp file manipulation from .net.
 
BTW, NET 2.0 introduces ftp client functionality, but some pieces are still missing. So I concider Wininet a better solution.

 
Lev Elbert
QuestionHow to put it in passive mode Pinsussadamloving28-Sep-05 7:56 
GeneralfunIConnect failed, error value: 3 Pinmemberjklpq23-Mar-05 3:14 
GeneralRe: funIConnect failed, error value: 3 PinmemberDCChristopher29-Sep-05 5:20 
GeneralRe: funIConnect failed, error value: 3 PinmemberMember 291684726-Feb-08 12:03 
GeneralRe: funIConnect failed, error value: 3 Pinmemberjasonsmithwick12-Jan-09 8:30 
GeneralConnection Timeout Problem PinmemberRattan25-Nov-04 19:41 
GeneralOverwrite locked file Pinmemberkholland1522-Nov-04 4:35 
GeneralFree, open-source pure C# FTP component PinsussHans Andersen11-Nov-04 9:15 
GeneralRe: Free, open-source pure C# FTP component Pinmembermaheshsingh21@yahoo.co.in1-Jul-05 19:39 
GeneralNLST using FtpCommand PinmemberRattan26-Sep-04 23:36 
GeneralRe: NLST using FtpCommand PinmemberMiataMan11-Nov-04 8:20 
GeneralRe: NLST using FtpCommand PinmemberJaze28-Sep-05 4:12 
GeneralAn Asembly Code for sending Data to server PinmemberZeeshan_lqp10-Sep-04 10:46 
Questionwin 98 / 2000 supported? PinmemberTim_Mackey17-Aug-04 2:56 
AnswerRe: win 98 / 2000 supported? PinmemberTim_Mackey19-Aug-04 4:12 
GeneralFtpCommand Pinmembergnanib15-Jun-04 9:30 
GeneralRe: FtpCommand Pinmembermarcinsulima7-May-05 12:55 
GeneralIncorrect password PinmemberUdaan200313-Feb-04 12:46 
GeneralProgress Bar PinmemberJeffrey Scott Flesher15-Dec-03 12:25 
QuestionAny other alternatives? Pinmembersxlu18-Nov-03 10:41 
AnswerRe: Any other alternatives? PinsussAnonymous29-Dec-03 6:07 
AnswerRe: Any other alternatives? Pinmemberkrprasad19-Jan-04 10:41 
GeneralFTP Connection not working Pinmembergirishnadig18-Sep-03 6:38 
GeneralDefault data port is 20 Pinmembercwkingjr17-Sep-03 19:05 
GeneralRe: Default data port is 20 PinmemberPeter Beedell17-Sep-03 20:33 
GeneralSource Files Download Link Broken Pinmemberfbougadou17-Sep-03 5:12 
GeneralRe: Source Files Download Link Broken PinmemberPeter Beedell17-Sep-03 20:34 

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
Web04 | 2.8.140721.1 | Last Updated 17 Sep 2003
Article Copyright 2003 by Peter Beedell
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid