Categories
How To

How To: Use the FtpWebRequest

Networking In The Hand includes a full desktop-compatible implementation of the FtpWebRequest class. This plugs into the WebRequest class so that calling WebRequest.Create() with an FTP Uri will create an object of type FtpWebRequest. Because FTP support isn’t built into the Compact Framework you have to register the class with this prefix using the the following code (only required once in your code):-


InTheHand.Net.FtpWebRequest.RegisterPrefix();


We have already looked at performing simple operations with the WebClient class. for more complex FTP operations you can use the FtpWebRequest directly. For example retrieving the modification date of a specific file:-


FtpWebRequest requestDate = (FtpWebRequest)FtpWebRequest.Create(requestUri);
requestDate.Method = WebRequestMethods.Ftp.GetDateTimestamp;
if (wc.Credentials != null)
{
   requestDate.Credentials = wc.Credentials;
}
FtpWebResponse responseDate = null;

responseDate = (FtpWebResponse)requestDate.GetResponse();

if (responseDate != null)
{
   MessageBox.Show(responseDate.LastModified.ToString());
   responseDate.Close();
}

By Peter Foot

Microsoft Windows Development MVP