WhiteWhiskers
WhiteWhiskers

Reputation: 1

Why won't the VB.NET function Hash and Download files from FTP Server using the given function?

So, I have a set of functions that run within a program that I designed, that acts as an integrity checker of sorts.

This is the process behind it:

It streams a text file from a remote FTP server, iterating through every line within the text file, and making the data on each line a value plugged into each index of a new array for each category of data.

Declaring the variables as public, so any part of the program can access the arrays.

Public directory_Structure_Array As String()
Public checksum_List_Array As String()
Public local_FileList_Array As String()
Public server_FileList_Array As String()

The code:

 **'-----Directory Structure-----

        Dim directory_StructureTXT_Stream As String =
        sr.DownloadString("ftp://phantom-net.synology.me/TechToolkit/Configuration/Integrity_Verification/directory_Structure.txt")
        Dim directory_Structure_Array As String() =
        directory_StructureTXT_Stream.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)**

The server text file: directory_Structure.txt

Tools
Tools\VR
Tools\VR\ENDITALL
Tools\TUNEUP
Tools\TUNEUP\CCleaner
Tools\TUNEUP\DiskDefragPro
Tools\TUNEUP\DiskDefragPro\Data
Tools\TUNEUP\DiskDefragPro\Lang
Tools\REPAIR
Tools\ANTIVIRUS_INSTALLERS
Tools\ANTIVIRUS_REMOVAL_TOOLS

The newly-created array:

Public directory_Structure_Array() As String =
    {"Tools",
     "Tools\VR",
     "Tools\VR\ENDITALL",
     "Tools\TUNEUP",
     "Tools\TUNEUP\CCleaner",
     "Tools\TUNEUP\DiskDefragPro",
     "Tools\TUNEUP\DiskDefragPro\Data",
     "Etc Etc Etc..."}

The Function:

'------------GENERATE THE NECESSARY TOOLKIT DIRECTORIES---------------------------------------------
        For Each Tool_Directory In directory_Structure_Array
            If My.Computer.FileSystem.DirectoryExists(Environment.CurrentDirectory & "\" & Tool_Directory) = False Then 'Check the existance of the directory
                Try
                    My.Computer.FileSystem.CreateDirectory(Environment.CurrentDirectory & "\" & Tool_Directory) 'Create the directory if it doesn't exist.
                    integrity_Listbox.Items.Add("Directory: " & Environment.CurrentDirectory & "\" & Tool_Directory & " created!")
                Catch ex As Exception
                    integrity_Listbox.Items.Add("Creating Directory: " & Environment.CurrentDirectory & "\" & Tool_Directory & " FAILED!")
                End Try
            Else
                'integrity_Listbox.Items.Add("Directory: " & Environment.CurrentDirectory & "\" & Tool_Directory & " already exists!")
            End If
        Next
    End Using
    '------------GENERATE THE NECESSARY TOOLKIT DIRECTORIES---------------------------------------

The Actual Problem:

You see, I have 4 text files that follow this structure of reading a text file, seperating each line, and putting each line of text into a string array, and the function above works perfectly! It finds out if the directories of the immediate program exist, and if they do not, it creates them. Works like a charm, BUT, the problem is, the code to perform the integrity check works PERFECTLY when using arrays hard-coded into the program itself, but will not work on the arrays created by the function above.

The function: that as seen below, is identical to the function above, it takes the checksum text file from the FTP server, and puts every checksum into a string array. I've even debugged it, to output every index of the array after it has been populated with strings from the text file, and all of the strings are there, as they are supposed to be........ but...

'-----Checksums-----
        Dim checksum_ListTXT_Stream As String =
        sr.DownloadString("ftp://phantom-net.synology.me/TechToolkit/Configuration/Integrity_Verification/checksum_List.txt")
        Dim checksum_List_Array As String() =
        checksum_ListTXT_Stream.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)

BUT...

When you run the function to check the integrity & existance of each file within the immediate directories, it reads the data from the newly created string arrays just fine, but it does not MD5 Hash them as it is supposed to do, and it does not download them if they are missing, and the debugger doesnt show anything when reaching the function below. The function seem below is where the whole toolkit kind of just goes stupid and doesnt do anything. It doesnt freeze, mind you, it just doesn't do anything anymore...

'------------VERIFY ALL FILES---------------------------------------------
    ''Dim indexOffset As Integer = 0
    For Each VR_Tool_Filename In local_FileList_Array 'Verify every file.
        ''indexOffset += 1 'Offset the selected hash index by one index every iteration of the function.

Loopback_IntegrityViolation:

            Me.integrity_Listbox.Items.Add(GetMD5(Environment.CurrentDirectory() & "\" & VR_Tool_Filename))
            If My.Computer.FileSystem.FileExists(Environment.CurrentDirectory() & "\" & VR_Tool_Filename) Then
                If GetMD5(Environment.CurrentDirectory() & "\" & VR_Tool_Filename) = checksum_List_Array(i) Then
                    integrity_Listbox.Items.Add(VR_Tool_Filename)
                    integrity_Listbox.Items.Add(GetMD5(Environment.CurrentDirectory() & "\" & VR_Tool_Filename) & " = Calculated MD5 Hash")
                    integrity_Listbox.Items.Add(checksum_List_Array(i) & " = Server-Expected MD5 Hash" & " | MD5 Hashes Match!")
                    integrity_Listbox.Items.Add("")
                Else
                    integrity_Listbox.Items.Add(VR_Tool_Filename)
                    integrity_Listbox.Items.Add(GetMD5(Environment.CurrentDirectory() & "\" & VR_Tool_Filename) & " = Calculated MD5 Hash")
                    integrity_Listbox.Items.Add(checksum_List_Array(i) & " = Server-Expected MD5 Hash" & " | Failed Hash Check!  Re-aquiring file from dataserver...")

                    'Delete the violation, then download the file integrity violation from the server, then re-evaluate it...
                    Try
                        My.Computer.FileSystem.DeleteFile(Environment.CurrentDirectory() & "\" & VR_Tool_Filename)
                        My.Computer.Network.DownloadFile("ftp://phantom-net.synology.me/TechToolkit/" & server_FileList_Array(i), Environment.CurrentDirectory() & "\" & VR_Tool_Filename, FTP_Login_Username, FTP_Login_Password, True, 7000, True)
                        integrity_Listbox.Items.Add("Successfully aquired " & server_FileList_Array(i) & " from dataserver.")
                        integrity_Listbox.Items.Add("")
                    Catch ex As Exception
                        integrity_Listbox.Items.Add("Failed to aquire " & server_FileList_Array(i) & " from dataserver.")
                        integrity_Listbox.Items.Add("")
                    End Try
                    'Flag the program to restart since it failed the initial integrity check.
                    integrity_Violation_Flag = True

                    'Loop back to re-evaluate the file integrity violation.
                    GoTo Loopback_IntegrityViolation
                End If

            Else 'If the file does not exist...
                integrity_Listbox.Items.Add(VR_Tool_Filename & " | File is missing!  Re-Aquiring from dataserver...")

                'Download the file from the data server.
                Try
                    My.Computer.Network.DownloadFile("ftp://phantom-net.synology.me/TechToolkit/" & server_FileList_Array(i), Environment.CurrentDirectory() & "\" & VR_Tool_Filename, FTP_Login_Username, FTP_Login_Password, True, 7000, True)
                    integrity_Listbox.Items.Add("Successfully aquired " & server_FileList_Array(i) & " from dataserver.")
                    integrity_Listbox.Items.Add("")
                Catch ex As Exception
                    integrity_Listbox.Items.Add("Failed to aquire " & server_FileList_Array(i) & " from dataserver.")
                    integrity_Listbox.Items.Add("")
                End Try
                'Flag the program to restart since it failed the integrity check.
                integrity_Violation_Flag = True

                'Loop back to re-evaluate the file integrity violation.
                GoTo Loopback_IntegrityViolation
            End If


            ''integrity_Listbox.TopIndex = integrity_Listbox.Items.Count - 1 'Offset the View to the bottom of the listbox every time an item is added.
        Next



    integrity_Listbox.Items.Add(" ------| Toolkit Integrity Check Complete |------")
    If integrity_Violation_Flag = True Then
        integrity_Listbox.Items.Add(" ------| One of more tools within the Toolkit have failed their integrity checks. |------")
        integrity_Listbox.Items.Add(" ------| You are required to restart the Toolkit. |------")
    Else
        integrity_Listbox.Items.Add(" ------| Integrity of all files has been successfully verified! |------")
        integrity_Listbox.Items.Add(" ------| You may now operate the Toolkit. |------")
    End If



    ''integrity_Listbox.TopIndex = integrity_Listbox.Items.Count - 1 'Offset the View to the bottom of the listbox.
    integrity_progressBar.Visible = False
    OK_Button.Enabled = True
End Sub

I need to know why it refuses to perform the MD5 Function on the array created by the function above, yet it works FINE, absolutely PERFECTLY, on a hard-coded array, even though both the hard-coded arrays and function-generated arrays are IDENTICAL in terms of data that they store.

This has been bothering me for over 4 hours, and I am going through all this effort because I want to simply modify a text file on the server, rather than re-compiling the program or having a file locally (which could be tampered with), to add directories and files as I expand the toolkit and it's tools.

If you need more code samples or data to help me, please let me know, I will provide you with whatever you need to help me figure this out.

ALSO, I am totally not against the idea of you suggesting a way to do this with XML, if there is a way to dynamically add new entries to an XML file, I think that's a MUCH more organized way of doing it, and I would not mind switching code over to do that. If you could iterate through the XML file, instead of making arrays, it would probably improve performance and centralize all the data into a single file, rather than 4 smaller text files as it is currently.

e.g.

<?xml version="1.0" standalone="yes"?>
    <Settings xmlns="http://tempuri.org/Settings.xsd">
      <Main>
        <VR_Tools>
          <Local_Filename>Tools\VR\adwcleaner.exe</Local_Filename>
          <Server_Filename>Core/Tools/VR/adwcleaner.exe</Server_Filename>
          <Checksum>1B151CCE618BE06C22B55FD4B502B75E</Checksum>
        </VR_Tools>
        <TUNEUP_Tools>
          <Local_Filename>Tools\TUNEUP\example.exe</Local_Filename>
          <Server_Filename>Core/Tools/TUNEUP/example.exe</Server_Filename>
          <Checksum>DDC58ACC1DE2D888D55562F1A71E840F</Checksum>
        <TUNEUP_Tools>
      </Main>
    </Settings>

Upvotes: 0

Views: 291

Answers (1)

WhiteWhiskers
WhiteWhiskers

Reputation: 1

So I found out that I needed to put "End Using" at the end of the "VERIFY ALL FILES" section.

Apparently when I had the End Using at the tail end of the "GENERATE THE NECESSARY TOOLKIT DIRECTORIES", it didnt allow the function to continue with integrity checking the rest of the toolkit's files. I am not entirely sure why this was, but further investigation will probably shed some light on the subject.

All in all, It's fixed the issue.

Old Version:

'------------GENERATE THE NECESSARY TOOLKIT DIRECTORIES---------------------------------------------
    For Each Tool_Directory In directory_Structure_Array
        If My.Computer.FileSystem.DirectoryExists(Environment.CurrentDirectory & "\" & Tool_Directory) = False Then 'Check the existance of the directory
            Try
                My.Computer.FileSystem.CreateDirectory(Environment.CurrentDirectory & "\" & Tool_Directory) 'Create the directory if it doesn't exist.
                integrity_Listbox.Items.Add("Directory: " & Environment.CurrentDirectory & "\" & Tool_Directory & " created!")
            Catch ex As Exception
                integrity_Listbox.Items.Add("Creating Directory: " & Environment.CurrentDirectory & "\" & Tool_Directory & " FAILED!")
            End Try
        Else
            'integrity_Listbox.Items.Add("Directory: " & Environment.CurrentDirectory & "\" & Tool_Directory & " already exists!")
        End If
    Next
End Using
'------------GENERATE THE NECESSARY TOOLKIT DIRECTORIES---------------------------------------

Fixed Version:

integrity_Listbox.Items.Add(" ------| Toolkit Integrity Check Complete |------")
        If integrity_Violation_Flag = True Then
            integrity_Listbox.Items.Add(" ------| One of more tools within the Toolkit have failed their integrity checks. |------")
            integrity_Listbox.Items.Add(" ------| You are required to restart the Toolkit. |------")
        Else
            integrity_Listbox.Items.Add(" ------| Integrity of all files has been successfully verified! |------")
            integrity_Listbox.Items.Add(" ------| You may now operate the Toolkit. |------")
        End If


        ''integrity_Listbox.TopIndex = integrity_Listbox.Items.Count - 1 'Offset the View to the bottom of the listbox.
        integrity_progressBar.Visible = False
        OK_Button.Enabled = True
    End Using

Upvotes: 0

Related Questions