FTP and ASP.NET 2.0

I recently played with the new FTP capabilities of ASP.NET 2.0.  I created several directories on my web server, each of which harbored multiple files(text and images).  My goal was to create identically named directories on my FTP server, and then send the contents of the original web server directories to their new FTP server counterparts.  All things considered, this is a nice addition to the.NET quiver. 

Goes a little something like this:

1.) Creating  directories on an FTP server that match directories on your web server.

Dim path As String = Server.MapPath(".\directory1\")

Dim s As String

'******Iterate through the directories. 

For Each s In Directory.GetDirectories(path)

'******Some of my typically sloppy string wrangling

Dim b As Array = s.Split("\")

s = Convert.ToString(b(6))

'******Define name of target FTP server, and the name of the directory that will be created there.

Dim URI As String = "ftp://ftpservername/” + s

Dim ftp As FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest)

'******Provide login info.  Security is job one. : )

= New System.Net.NetworkCredential("username", "password")

'******Specify whether the connection should be kept alive. Default value is true.

= False

'******Why are we here?  In this case, to create a directory on the target FTP server.

= WebRequestMethods.Ftp.MakeDirectory

Using response As System.Net.FtpWebResponse = CType(, System.Net.FtpWebResponse)

response.Close

End Using

Next

2.) Sending files from directories on your web server to identically named (See step 1) directories on an FTP server.

Dim d As String

'******Iterate through the directories. 

For Each d In Directory.GetDirectories(path)

'******Some of my typically sloppy string wrangling

Dim b As Array = d.Split("\")

d = Convert.ToString(b(6))

Dim f As String

'******Iterate through the files in the current directory

For Each f In Directory.GetFiles(path + d)

Dim c As Array = f.Split("\")

f = Convert.ToString(c(7))

'******Define name of target FTP server, the target FTP directory (as created in Step 1.), and the name of the file that will be sent there.

Dim URI As String = "ftp://ftpservername/ + d + “/“ + f“

'******Define the FtpWebRequest

Dim ftp As System.Net.FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest)

ftp.Credentials = New System.Net.NetworkCredential("username", “password")

ftp.KeepAlive = False

ftp.UseBinary = True

'******Why are we here?  In this case, to upload a file to the target FTP server.

ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile

'******Stream bytes from web server file

Dim fs As New FileStream(path + d + "/" + f, FileMode.Open)

Dim filecontents(fs.Length) As Byte

fs.Read(filecontents, 0, fs.Length)

fs.Close

Dim requestStream As Stream = ftp.GetRequestStream

requestStream.Write(filecontents, 0, filecontents.Length)

requestStream.Close

Dim response As FtpWebResponse = ftp.GetResponse

response.Close

Next

Next

This article is part of the GWB Archives. Original Author: Michael McMorrow

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.