We help IT Professionals succeed at work.

VB.NET FTP problem.. get same file all the time?

Hi I have some problem whit some code...

When I run this code it works perfect the first time every time the application runs..
But the next time it get´s the same "old file" as it took the first time.. I think I don´t do a proper "close ftp" but can´t figure out what I do wrong.

If I close the application down and then start it then it works...
The problem is it only get the file one time..

Please give me some help..
''' <summary>
    ''' Get FTP
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Download_Files()
      
        Try

            Dim INet, INetConn As Integer
            ' Kontrollbit visa hämtning ok osv..
            Dim RC As Boolean

            'Skapar FTP-communicationen FTPAdress
            INet = InternetOpen("FTP Connection", 1, vbNullString, vbNullString, 0)
            INetConn = InternetConnect(INet, FTPAdress, 21, "test", "test", 1, 0, 0)


            'Filer som skall hämtas..
            LARMFile = "test1.csv"
            ' Trendfil som skall hämtas
            RC = FtpGetFile(INetConn, "/TRENDS/test1.SKV", TempFiles + NewFile, False, 1, 0, 0)
            ' Trendfil som skall hämtas
            LARMFile2 = "test2.csv"
            FtpGetFile(INetConn, "/TRENDS/test2.SKV", TempFiles + NewFile2, False, 1, 0, 0)

            

            ' Show if get have been correct
            If RC Then
                Me.uxStatus.Text = "bla bla bla successfully!"
            End If
            'Close the FTP communicationen...
            InternetCloseHandle(INetConn)
            InternetCloseHandle(INet)

            ' Set the new file adress
            Dim fFile As New FileInfo(TempFiles + NewFile)
            If Not fFile.Exists Then
                ' MessageBox.Show("No file exits")

            Else
                Dim aa As Long
                aa = GetAttr(TempFiles + NewFile)
                Debug.Print("before=" & aa)
                aa = (aa And 127) Xor vbReadOnly
                Debug.Print("set to " & aa)
                SetAttr(TempFiles + NewFile, aa)
                aa = GetAttr(TempFiles + NewFile)
                Debug.Print("after=" & aa)
            End If

            ' Sätter filens adress
            Dim fFile3 As New FileInfo(TempFiles + NewFile2)
            If Not fFile3.Exists Then
                ' MessageBox.Show("Finns ingen fil...")

            Else
                Dim aa As Long
                aa = GetAttr(TempFiles + NewFile2)
                Debug.Print("before=" & aa)
                aa = (aa And 127) Xor vbReadOnly
                Debug.Print("set to " & aa)
                SetAttr(TempFiles + NewFile2, aa)
                aa = GetAttr(TempFiles + NewFile2)
                Debug.Print("after=" & aa)
            End If

        Catch ex As Exception
            'MsgBox(ex.ToString)
            Me.uxStatus.Text = "FTP-Failed"
        End Try


    End Sub

Open in new window

Comment
Watch Question

Most Valuable Expert 2012
Top Expert 2008

Commented:
This looks like VB6 code.  If you are using VB.NET, would you like to learn how to use an FtpWebRequest to FTP files?
Shahan AyyubSenior Software Engineer

Commented:
could you please check out by changing line 33 and 34 like this:

     dim b1 as boolean =      InternetCloseHandle(INetConn)
     dim b2 as boolean =      InternetCloseHandle(INet)

and verify that the handle is closed successfully or not.

Author

Commented:
TheLearnedOne
Yes please give me some tips!

Shaha.. I Will test that! Tank you :)
Most Valuable Expert 2012
Top Expert 2008
Commented:
How to: Download Files with FTP
http://msdn.microsoft.com/en-us/library/ms229711.aspx
Imports System.IO
 Imports System.Net
 Imports System.Text
 Namespace Examples.System.Net
     Public Class WebRequestGetExample
         Public Shared Sub Main()
             ' Get the object used to communicate with the server.
             Dim request As FtpWebRequest = TryCast(WebRequest.Create("ftp://www.example.com/"), FtpWebRequest)
             request.Method = WebRequestMethods.Ftp.DownloadFile
             ' This example assumes the FTP site uses anonymous logon.
             request.Credentials = New NetworkCredential("anonymous", "example@example.com")
             Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
             Dim responseStream As Stream = response.GetResponseStream()
             Dim reader As New StreamReader(responseStream)
             Console.WriteLine(reader.ReadToEnd())
             Console.WriteLine("Download Complete, status {0}", response.StatusDescription)
             reader.Close()
             response.Close()
         End Sub
     End Class
 End Namespace

Open in new window

Author

Commented:
TheLearnedOne:

hi.. thank you for your example..
but in your example where do I declare the file I want to download?
tex if I want to download  ""/TRENDS/test1.SKV"

Author

Commented:
I find how I did when I hade your code..  thank you verry mutch.,.  works like a charm..
Most Valuable Expert 2012
Top Expert 2008

Commented:
If you need to specify the file name in the download, then it should be a part of the URL when creating the web request.