sábado, 16 de mayo de 2009

FTP con VB.NET

mucha, despues de quebrarme la cabeza....
estaba haciendo un cliente FTP usando un modelo de VB6 que utiliza el contro Inet, pues dije, copy paste! ahuuuueeeevoooos.....
siemple y sencillamente no funciono, tos que me quedaba?...matarme a buscar codigo y encontre esto:
http://www.devasp.net/net/articles/display/246.html

ahi esta como hacer esa cosa y sin controles raros..
por si ya no esta el link

First you need to create an instance of the FtpWebRequest class and set the ftp address of the folder you want to get list from.

Dim fwr As FtpWebRequest = FtpWebRequest.Create(“ftp://ftp.google.com”)

Then you need to set the user name and password for accessing the FTP server. There is a property called Credentials in the FtpWebRequest class that is of type ICredential and you can set it to an object of type NetworkCredential object like this.

fwr.Credentials = New NetworkCredential(“userName”, “password”)

Then you need to specify the Method of this class which is an instance of WebRequestMethods object like this.

fwr.Method = WebRequestMethods.Ftp.ListDirectory

After specifying these properties I’ll retrieve the list of files and folders using a Stream reader and display that in the console. The code for this is as follows.

Dim sr As New StreamReader(fwr.GetResponse().GetResponseStream())

Dim str As String = sr.ReadLine()

While Not str Is Nothing

Console.WriteLine(str)

str = sr.ReadLine()

End While

sr.Close()

sr = Nothing

fwr = Nothing

This will be the simplest example of using these classes to work with the file transfer protocol. Try to work with the code, change it and see what happens. Also find about all the members of this class from the MSDN library.