为什么VB.NET不会使用给定的函数从FTP服务器中使用哈希和下载文件?

时间:2014-10-08 23:48:05

标签: xml vb.net ftp streamreader

所以,我有一组在我设计的程序中运行的函数,它充当各种完整性检查器。

这是它背后的过程:

它从远程FTP服务器流式传输文本文件,遍历文本文件中的每一行,并使每行上的数据成为插入每个数据类别的新数组的每个索引的值。

将变量声明为公共变量,因此程序的任何部分都可以访问数组。

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

代码:

 **'-----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)**

服务器文本文件: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

新创建的数组:

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..."}

功能:

'------------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---------------------------------------

实际问题:

你知道,我有4个文本文件遵循这种阅读文本文件的结构,分隔每一行,并将每行文本放入一个字符串数组,上面的函数完美无缺!它会发现是否存在直接程序的目录,如果不存在,则会创建它们。像魅力一样工作,,问题是,当使用硬编码到程序本身的数组时,执行完整性检查的代码完美地工作,但不能在上面函数创建的数组上工作。

如下所示的函数与上面的函数相同,它从FTP服务器获取校验和文本文件,并将每个校验和放入字符串数组中。我甚至调试了它,在用文本文件中的字符串填充之后输出数组的每个索引,并且所有的字符串都在那里,因为它们应该是........ 但...

'-----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 ...

运行该功能以检查完整性&在直接目录中的每个文件的存在,它从新创建的字符串数组中读取数据就好了,但它不是MD5哈希它们应该做的,并且如果它们丢失则不下载它们,并且调试器达到下面的功能时没有显示任何内容。下面的函数似乎是整个工具包只是变得愚蠢而且什么都不做的地方。它没有冻结,请注意,它只是不再做任何事了......

'------------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

我需要知道为什么它拒绝在由上面的函数创建的数组上执行MD5函数,但它在硬编码数组上运行FINE,绝对完美,即使两个硬编码数组也是如此和函数生成的数组在它们存储的数据方面是IDENTICAL。

这困扰了我超过4个小时,我正在经历所有这些努力,因为我想简单地修改服务器上的文本文件,而不是重新编译程序或在本地拥有文件(可能是篡改),在我扩展工具包及其工具时添加目录和文件。

如果您需要更多代码示例或数据来帮助我,请告诉我,我会为您提供帮助我解决此问题的所有内容。

另外,我完全不反对你建议用XML做这个方法的想法,如果有办法动态地将新条目添加到XML文件中,我认为这是一种更有组织的方式,我不介意切换代码来做那件事。如果您可以遍历XML文件而不是制作数组,那么它可能会提高性能并将所有数据集中到一个文件中,而不是像目前那样将4个较小的文本文件集中。

例如

<?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>

1 个答案:

答案 0 :(得分:0)

所以我发现我需要在&#34;验证所有文件&#34;结尾处放置&#34; End Using&#34; 。部分。

显然,当我在“生成必要的工具包指南”的尾端进行最终使用时,它不允许该功能继续完整性检查工具包的其余文件。我不完全确定为什么会这样,但进一步的调查可能会对这个问题有所了解。

总而言之,它解决了这个问题。

旧版本:

'------------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---------------------------------------

修正版:

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