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

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

By , 16 Sep 2003
 

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
Member
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!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionUnknown error code: 1008memberdba@tom30 Aug '11 - 23:04 
RantInternetOpen Bugmemberjbuehler25 Jan '10 - 15:05 
Not sure if anyone else ran into this but I found this code throws exceptions if you try to open a connection twice in the same instance of a program.
 
Better put by an example:
FtpDll ftp = new FtpDll();
            ftp.funIConnect(); // uses default connection settings
            ftp.funFTPChangeDir("/tffs0");
            ftp.funFTPPutFile("codeldr", "codeldr");
            ftp.funFTPDisconnect();
 
            ftp.funIConnect(); // will throw an exception here
            ftp.funFTPChangeDir("/tffs0/scripts"); // doesn't make it here
            ftp.funFTPDisconnect();
 
If you wonder why I connect/disconnect twice, it's the same as if I clicked a button twice in my GUI. I traced the problem to the funIOpen() function, specifically the call to wininet.dll's InternetOpen() function.
public void funIOpen()
        {
            string strProxy = "";
            string strProxyBypass = "";
            try
            {
                funClearError(); //clear the 'last error' value
                hInternetSession = FtpDll.InternetOpen("FTPTest", FtpDll.OPEN_TYPE_DIRECT, strProxy, strProxyBypass, 0);
                if (funGetError())
 
I looked up the definition of InternetOpen on MSDN(see http://msdn.microsoft.com/en-us/library/aa385096%28VS.85%29.aspx[^])
Basically it states that for the proxy and proxybypass "Do not use an empty string, because InternetOpen will use it as the proxy name." HMMMM. Big red light should go off here.
 
Changing these two lines clears the problem right up. The MSDN states "If dwAccessType is not set to INTERNET_OPEN_TYPE_PROXY, this parameter is ignored and should be NULL." For once microsoft did help.
string strProxy = null;
     string strProxyBypass = null;
 
Hopefully this helps anyone still stumbling onto this via Google, much as I did.
GeneralMy vote of 2mvpJohn Simmons / outlaw programmer8 Jan '09 - 9:26 
GeneralRe: My vote of 2memberElmue11 Jan '09 - 5:23 
GeneralA comfortable FTP class in .NETmemberElmue27 Aug '08 - 12:22 
GeneralRe: A comfortable FTP class in .NETmvpJohn Simmons / outlaw programmer8 Jan '09 - 9:27 
GeneralRe: A comfortable FTP class in .NETmemberElmue11 Jan '09 - 5:12 
QuestionPossibility to use QUOTE RCMD SBMJOB CMD()?memberpaoloden6 Jan '07 - 0:58 
Generalget every file of a directorymemberwilco9623 Feb '06 - 7:22 
AnswerRe: get every file of a directorymemberantgao9 Nov '06 - 4:21 
GeneralRe: get every file of a directorymembergan.gary15 Sep '08 - 23:39 
Generalimportant change required for using in .Net 2.0...memberTim_Mackey8 Feb '06 - 2:55 
GeneralRe: important change required for using in .Net 2.0...membershareeef8 Dec '06 - 8:52 
GeneralRe: important change required for using in .Net 2.0...memberVictortto16 Oct '07 - 13:08 
GeneralRe: important change required for using in .Net 2.0...memberTriple-S21 Oct '07 - 0:52 
RantRe: important change required for using in .Net 2.0...memberHAIXJ28 May '08 - 1:09 
GeneralBugmemberelbertlev8 Jan '06 - 6:52 
QuestionHow to put it in passive modesussadamloving28 Sep '05 - 7:56 
GeneralfunIConnect failed, error value: 3memberjklpq23 Mar '05 - 3:14 
GeneralRe: funIConnect failed, error value: 3memberDCChristopher29 Sep '05 - 5:20 
GeneralRe: funIConnect failed, error value: 3memberMember 291684726 Feb '08 - 12:03 
GeneralRe: funIConnect failed, error value: 3memberjasonsmithwick12 Jan '09 - 8:30 
GeneralConnection Timeout ProblemmemberRattan25 Nov '04 - 19:41 
GeneralOverwrite locked filememberkholland1522 Nov '04 - 4:35 
GeneralFree, open-source pure C# FTP componentsussHans Andersen11 Nov '04 - 9:15 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 17 Sep 2003
Article Copyright 2003 by Peter Beedell
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid