-1

I've been struggling with trying to read the configuration files on a device via FTP. The files are generated dynamically when the FTP request is sent, and the device does not support the FTPGetFileSize command.

I've lost count of the number of examples from the Web that I've tried in an attempt to solve this. Part of the problem is likely due to my inexperience with file streams. So, I'm looking for a kind soul that might be able to provide some insight.

In the code below, I'm trying two different methods. The first is a straight read of the entire file, the second attempts to control the read based whether there is anything coming in over the stream.

Both methods work on other devices that support the FTPGetFileSize command. But neither method works for this device.

Project form

Imports are:

Imports System.Net
Imports System.IO
Imports System.Text

The cmdGet_Click event code looks like this:

        Dim sLocal_Path As String = "C:\Temp\FTP_Test\" + txtIPAddress.Text + "\"
        System.IO.Directory.CreateDirectory(sLocal_Path)

        ' Complete the local path
        sLocal_Path += txtFileName.Text
        If System.IO.File.Exists(sLocal_Path) Then
            System.IO.File.Delete(sLocal_Path)      '  Make sure we don't have a collision.
        End If

        ' Set up the Remote Path.
        Dim sRemote_Path As String = "ftp://" + txtIPAddress.Text + "/gen:" + txtFileName.Text

        Dim request As FtpWebRequest = CType(WebRequest.Create(sRemote_Path), FtpWebRequest)
        request.Method = WebRequestMethods.Ftp.DownloadFile

        request.Credentials = New NetworkCredential(FTP_User, FTP_Password)
        request.UsePassive = True
        request.Timeout = 15000
        request.UseBinary = False

        Dim response As FtpWebResponse = CType(request.GetResponse(), FtpWebResponse)
        Dim responseStream As Stream = response.GetResponseStream()

        Dim reader As StreamReader = New StreamReader(responseStream)
        Dim writeFile As StreamWriter = New StreamWriter(sLocal_Path)

        If RadioReadToEnd.Checked Then
            ' Write the FTP stream to the target file
            writeFile.Write(reader.ReadToEnd()) ' works with static files.

        Else
            '  Write the FTP stream as long as we have data coming in.
            Dim ByteCount As Integer = 0
            Threading.Thread.Sleep(500)

            While Not (reader.Peek() = -1)
                ByteCount += 1
                If ByteCount > 4095 Then    ' Go up for air after every block.
                    ByteCount = 0           ' Reset the byte counter for the next block.
                    Application.DoEvents()
                End If
                writeFile.Write(Chr(reader.Read))
            End While
        End If

        reader.Close()
        reader.Dispose()

        writeFile.Close()
        writeFile.Dispose()

        responseStream.Close()
        responseStream.Dispose()

Note that I put a delay in just before the While Loop in the hopes that maybe something would show up in the stream to keep the loop from terminating. Apparently I need some way to prime the pump.

Edit: The DOS FTP Get works fine. My assumption is that it has some way to get around the FTPGetFileSize not being supported. Is there a way to get the WebClient FTP Methods to do the same thing with a dynamically generated file?

I need a nudge (okay, a kick) in the right direction. Any suggestions are greatly appreciated!

MarkW
  • 1
  • 3
  • I forgot to mention two things. 1. The DOS FTP Get command works fine with this same device and the configuration files. 2. Both pieces of logic shown here also work fine with this device when used on static instead of dynamic files. – MarkW Jul 18 '18 at 23:11
  • Why it does not work? What problem do you experience? Show us [log file](https://stackoverflow.com/q/9664650/850848). – Martin Prikryl Jul 21 '18 at 06:31

1 Answers1

0

It is now working! The problem turned out to be a missing '/' at the root of the path.

This particular device has a drive reference in it's path that other devices my routine talks to did not have. So, between that and the error being thrown regarding the file size, I was put off of the scent.

Thanks to Martin Prikryl's comment about showing a log, I went off to create a Wireshark recording, and that's when I noticed the missing slash when I compared it to the DOS FTP Get recording. So, while I'm a little red-faced, I hope my experience helps someone else who runs into a similar problem. But mostly, I'm very relieved that I did not have to go the route of writing an FTP Client that dealt with raw commands such as POST and RETR! The WebClient tools are now working very well for this application.

MarkW
  • 1
  • 3