Link to home
Start Free TrialLog in
Avatar of bdunz19
bdunz19

asked on

Connecting to an FTP server with VB .NET

I can't seem to find winsock controls or anything like that with VB .NET, and I'm at a loss as to how I would connect to a server using FTP. Hopfully someone can help. All I need to do is connect to a server and make a directory. That's it.
Thanks!
Avatar of mquiroz
mquiroz

checkout this FREE activeX control:

www.chilkatsoft.com/ChilkatFtp.asp 

it's very, very easy to use...
Avatar of Bembi
Everything you need is included in the operation system. You can use the winsock or wininet functions. For the wininet I have some VB6 code (it should no be the problem to use it in .Net, the only problem is, its about 600 lines long (it includes a few functions I used for automatic software updates from the internet using FTP or HTTP and is configurable).

If you want to have a look at the whole code, let me know, I will store it on my web-site or send you by mail.

What's to do is.

Open a connection to the internet
Open a session to a target system
Send requests to this system
or
Use the FTP-commands
Close the session
Close the connection


The main functions for wininet (API Calls) are:

To establish the connection and session

Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long

Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long


Ftp function

Private Declare Function FtpOpenFile Lib "wininet.dll" Alias "FtpOpenFileA" (ByVal hConnect As Long, ByVal sFileName As String, ByVal lAccess As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long

Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long

Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hFtpSession As Long, ByVal lpszLocalFile As String, ByVal lpszRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Long

Private Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hConnect As Long, ByVal lpszFileName As String) As Long

Private Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long

Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hConnect As Long, ByVal lpszFileName As String) As Long

Private Declare Function FtpSetDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Long

Private Declare Function FtpGetDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Long

Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer

Private Declare Function InternetWriteFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer

Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long

Private Declare Function InternetQueryDataAvailable Lib "wininet.dll" (ByRef hFile As Long, ByRef lpdwNumberOfBytesAvailable As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long

to close whatever to close

Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
Hi,
VB.net has a System.network.socket and System.network.stream classes which do the same thing with better performance as they as multithreaded.
Dim newSocket As System.Net.Sockets.Socket
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello,
try several free libraries found at www.codeproject.com. You could also try Rebex FTP.net component - it comes with a lot of samples (both vb.net and c#). It even includes WinForm ftp client similar to Windows Commander (aka Total Commander) with complete source code...

http://www.rebex.net/ftp.net/

Martin Vobr