Subscribe Now

ABC, 123, Ruby, C#, SAS, SQL, TDD, VB.NET, XYZ

Monday, October 29, 2007

Microsoft .NET (2.0) FTP Support Still Sucks

An FTP client is coaxed out of the .NET 1.1 framework by Liberty (& Enterprise Distributed Technologies –LOL) and Howard Richards shows how to get the job done with .NET 2.0. Whidbey (2.0) was billed as having FTP support, but when you have to write this code does that qualify as support?
string URI = "ftp://mirror.x10.com/gnuftp/gnu/README.DESCRIPTIONS";
System.IO.FileInfo fi = new System.IO.FileInfo(@"c:\temp\deleteme.txt");
System.Net.FtpWebRequest ftp =
(System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(URI);
ftp.Credentials = new System.Net.NetworkCredential("anonymous", "");
ftp.KeepAlive = false;
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;
ftp.UseBinary = true;
using (System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)ftp.GetResponse())
{
using (System.IO.Stream responseStream = response.GetResponseStream())
{
using (System.IO.FileStream fs = fi.OpenWrite())
{
try
{
byte[] buffer = new byte[2048];
int read = 0;
do
{
read = responseStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, read);
} while (!(read == 0));
responseStream.Close();
fs.Flush();
fs.Close();
}
catch (Exception)
{
fs.Close();
fi.Delete();
throw;
}
}
responseStream.Close();
}
response.Close();
}

It almost makes more sense to shell out to c:\windows\system32\ftp.exe as in this VBA code.
Dim ff As Integer
ff = FreeFile
ff = FreeFile
Open "c:\temp\ftpcommands.scr" For Output As #ff
Print #ff, "open mirror.x10.com"
Print #ff, "anonymous"
Print #ff, "no_password"
Print #ff, "cd gnuftp/gnu"
Print #ff, "binary"
Print #ff, "get README.DESCRIPTIONS c:\temp\deleteme3.txt"
Print #ff, "bye"
Close #ff
ff = FreeFile
Open "c:\temp\ftpcommands.bat" For Output As #ff
Print #ff, "ftp -s:c:\temp\ftpcommands.scr"
Print #ff, "Echo ""Complete"" > c:\temp\ftpcommands.done"
Close #ff
Shell "c:\temp\ftpcommands.bat", vbHide
Do While Dir("c:\temp\ftpcommands.done") = ""
DoEvents
Loop
Dim dtmContinueAt As Date
dtmContinueAt = Now + TimeValue("0:00:03")
Do While Now < dtmContinueAt
Loop
Kill "c:\temp\ftpcommands.scr"
Kill "c:\temp\ftpcommands.bat"
Kill "c:\temp\ftpcommands.done"

Now contemplate this equivalent Ruby code.
require 'net/ftp'
Net::FTP.open('mirror.x10.com') do |ftp|
ftp.login
files = ftp.chdir('gnuftp/gnu')
ftp.getbinaryfile('README.DESCRIPTIONS', 'c:\temp\deleteme2.txt')
end

I feel prompted to ask, just wtf does Microsoft have against FTP? Aside from that, the obvious question is “will .NET 3.x ‘Orcas’ do better?” Or maybe the obvious question is, “is .NET a misnomer*?”

* Because last time I checked FTP was an Internet protocol: “a system for transferring computer files especially via the Internet.”

2 comments:

BorgMusings said...

I am not a computer programmer. In fact, I am one of those people who might obtain a tiny little bit of Access knowledge and become a great danger. However, I was intrigued by the name of the blog site and "Microsoft.NET (2.0) FTP Support Still Sucks".

Okay, so I didn't understand the code, but it was clear what was going on. I, like most of us, have the MS ball-and-chain around my neck. This leaves me and programmers working on my projects spending hours trying to figure out how to do something that should logically work with just a few lines of code, but doesn't b/c the Borg didn't include the functionality. Instead a ton of gibberish (to the uninitiated) is required where a few lines of code would have sufficed, if the Borg had done "it" the right way or left the collective ego out of it.

So what is it, do they not know what they are doing, or is the collective ego so strong that it keeps them from doing what is right?

Anonymous said...

Here in 2012, that kludgy mess is still the way Micro$hit wants us to write the ftp access code.