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. : )
ftp.Credentials = New System.Net.NetworkCredential("username", "password")
'******Specify whether the connection should be kept alive. Default value is true.
ftp.KeepAlive = False
'******Why are we here? In this case, to create a directory on the target FTP server.
ftp.Method = WebRequestMethods.Ftp.MakeDirectory
Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, 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
19 December, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment