Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Need help in FTP from VB.net/asp.net - sample code included


Message #1 by "Neel" <neelbh@y...> on Fri, 10 Jan 2003 19:29:50
I am working on a .NEt project (VB.net)
I have to pick up files from remote server every hour
this will be a windows service
files are in 
Folder A (which has sub folders 1 & 2)
Folder B (which has sub folders 1 & 2)
in root directory

i am using a timer to trigger the event in .NEt
what would be the best way to do it ?
I was thinking of using the windows API using import dll's

i saw some where on discussion boards not to use Wininet.dll on the .NET 
platform

below is some of my code (please help me out)
all the paths mentions are from config settings
--------------------------------------------------------------------
Public Sub GetRemoteFiles()
        Try
               'Get files from remote server
            Dim FILE_ATTRIBUTE_NORMAL = &H80

            iOpen = 0
            iConnection = 0
            strFileFilter = "*.*"
            iOpen = API.InternetOpen("FTP Get", 0, vbNullString, 
vbNullString, 0)
            If iOpen Then
                iConnection = fnGetFTPConnection(iOpen)
                If iConnection Then
                    iFindFile = API.FtpFindFirstFile(iConnection, 
strFileFilter, pFileData, 0, 0)
                    If iFindFile Then
                        blnInetFoundFile = True
                    Else
                        blnInetFoundFile = False
                        Throw New Exception("blnInetFoundFile False")
                    End If
                    Do While blnInetFoundFile
                        strFileName = pFileData.cFileName
                        If strFileName <> "" Then
                            strFileName = Left(strFileName, InStr(1, 
strFileName, Chr(0), vbBinaryCompare) - 1)
                        End If
                        blnFtpGetFile = API.FtpGetFile(fnGetFTPConnection
(iOpen), strFileName, FROM_VENDOR_PATH & strFileName, False, 
FILE_ATTRIBUTE_NORMAL, 1, 0)
                        If blnFtpGetFile Then
                            blnFtpDelFile = API.FtpDeleteFile
(fnGetFTPConnection(iOpen), strFileName)
                            If Not blnFtpDelFile Then
                                'Throw New Exception("File Delete Failed")
                            End If
                            'Transfer sucess write to xml
                            Else
                            'Transfer_Failed write to xml
                           'Throw New Exception("File Get Failed")
                        End If
                        blnInetFoundFile = API.InternetFindNextFile
(iFindFile, pFileData)
                    Loop
                Else
                    Throw New Exception("FTP iConnection Failed")
                End If
            Else
                Throw New Exception("FTP iConnect Failed")
            End If
            ' Else
            'Throw New Exception("FTP Internet Open Failed")
            'End If
            'FileInProcess.WaitForExit()
        Catch ex As Exception
            Dim EBPPBNKEventLog As New EventLog()
            EBPPBNKEventLog.WriteEntry("GETREMOTEFILES", ex.Message & " -
 " & ex.StackTrace.ToString(), EventLogEntryType.FailureAudit)
            Finally
            iOpen = 0
            iConnect = 0
        End Try
        
    End Sub

Private Function fnGetFTPConnection(ByVal iOpen As Long) As Long
        Dim blnSetCurDir As Boolean
        iConnect = 0
        iConnect = API.InternetConnect(iOpen, REMOTE_SERVER_NAME, 0, 
REMOTE_USERID, REMOTE_PASSWORD, 1, 0, 0)
        REMOTE_INSOURCEFILE_PATH = deSlash(REMOTE_INSOURCEFILE_PATH)
        'If REMOTE_INSOURCEFILE_PATH = vbNullString Then
        'blnSetCurDir = True
        'Else
        blnSetCurDir = API.FtpSetCurrentDirectory(iOpen, 
REMOTE_INSOURCEFILE_PATH)
        'End If
        If blnSetCurDir Then
            fnGetFTPConnection = iConnect
        Else
            fnGetFTPConnection = 0
            Throw New Exception("blnSetCurDir False")
        End If
    End Function

-------------------------------------------------------------------

  Return to Index