Search
Monday, January 05, 2009 ..:: Blog ::.. Register  Login
 Search_Blog Minimize

 Print   
 Blog_Archive Minimize

 Print   
 FTP and .NET Minimize
Location: BlogsDevelopments    
Posted by: TM Central 9/7/2007 2:03 PM
With all of the abstraction in .NET and with all of the other "higher-level"" protocols such as SMTP present, it is a little amazing there is no support for FTP natively. Instead you need to access the Wininet api directly as with the following code:

Public Class FTP
EntryPoint:="InternetCloseHandle")> _
Public Shared Function CloseConnection(ByVal _
HINet As Integer) As Integer
End Function
EntryPoint:="InternetOpenA")> _
Public Shared Function Open(ByVal _
sAgent As String, ByVal lAccessType _
As Integer, ByVal sProxyName As String, _
ByVal sProxyBypass As String, _
ByVal lFlags As Integer) As Integer
End Function
EntryPoint:="InternetConnectA")> _
Public Shared Function Connect( _
ByVal hInternetSession As Integer, _
ByVal sServerName As String, _
ByVal nServerPort As Integer, _
ByVal sUsername As String, _
ByVal sPassword As String, _
ByVal lService As Integer, _
ByVal lFlags As Integer, _
ByVal lContext As Integer) As Integer
End Function
EntryPoint:="FtpPutFileA")> _
Public Shared Function PutFile( _
ByVal hFtpSession As Integer, _
ByVal lpszLocalFile As String, _
ByVal lpszRemoteFile As String, _
ByVal dwFlags As Integer, _
ByVal dwContext As Integer) As Boolean
End Function
EntryPoint:="FtpGetFileA")> _
Public Shared Function GetFile( _
ByVal hFtpSession As Integer, _
ByVal lpszRemoteFile As String, _
ByVal lpszNewFile As String, _
ByVal fFailIfExists As Boolean, _
ByVal dwFlagsAndAttributes As Integer, _
ByVal dwFlags As Integer, _
ByVal dwContext As Integer) As Boolean
End Function
End Class


Then you can access the API by calling the code as follows:

Try
' Instantiate a new FTP object
myFtpClient = New ftp
hInetSession = myFtpClient.Open( _
"WebClientFtpTransfer", 0, _
vbNullString, vbNullString, 0)
' Check to see if Internet session was
' successful
If hInetSession = 0 Then
Throw New System.Exception( _
"Could Not Connect to the Internet")
End If

' Now connect to the FTP client (include login
' if necessary)
hFTPSession = myFtpClient.Connect(hInetSession, _
ServerName, 21, FTPLogin, FTPPassword, 1, _
0, 0)
' Check to see if Internet session was successful
If hFTPSession = 0 Then
Throw New System.Exception( _
"Could Not Connect to the FTP Server:" & _
ServerName)
End If
' Now post the file
ftpFunctionRC = myFtpClient.PutFile(hFTPSession, _
strFileName, strFileName, 0, 0)

' Get the results from the remote server
If ftpFunctionRC = 0 Then
lblStatus.Text = "FTP Transfer Successful"
Else
Throw New System.Exception(M _
"Could Not Transfer File: " & strFileName)
End If
Catch ex As Exception
lblStatus.Text = "ERROR UPLOADING FILE " & _
"VIA FTP" & vbCrLf & ex.Message
lblStatus.ForeColor = System.Drawing.Color.Red
End Try
' Close the connection
myFtpClient.CloseConnection(hInetSession)


Permalink |  Trackback

  
Copyright 2005 by TMCentral, LLC   Terms Of Use  Privacy Statement
DotNetNuke® is copyright 2002-2009 by DotNetNuke Corporation