变量''在被赋值之前使用。

时间:2017-07-09 14:16:19

标签: vb.net

我正在尝试制作一个程序,下载一堆域并添加它们的Windows主机文件,但我遇到了一些麻烦。当我尝试将它们存储在列表中时,我一直收到错误。我不明白为什么它不起作用。

Sub Main()
    Console.Title = "NoTrack blocklist to Windows Hosts File Converter"
    Console.WriteLine("Downloading . . . ")
    Dim FileDelete As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "/Downloads" & "/notracktemp.txt"
    If System.IO.File.Exists(FileDelete) = True Then
        System.IO.File.Delete(FileDelete)
    End If
    download()
    Threading.Thread.Sleep(1000)
    Dim s As New IO.StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "/Downloads" & "/notracktemp.txt", True)
    Dim tempRead As String ' = s.ReadLine
    Dim tempSplit As String() ' = tempRead.Split(New Char() {" "})
    Dim i As Integer = 0
    Dim tempStore As String()
    s.ReadLine()
    s.ReadLine()
    Do Until s.EndOfStream = True
        tempRead = s.ReadLine
        tempSplit = tempRead.Split(New Char() {" "})
        Console.WriteLine(tempSplit(0))
        tempStore(i) = tempSplit(0)'The part that gives me the error
        i = i + 1
    Loop
    Console.ReadKey()
End Sub


Sub download()
    Dim localDir As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
    '"Enter file URL"
    Dim url As String = "https://quidsup.net/notrack/blocklist.php?download"
    '"Enter directory"
    Dim dirr As String = localDir & "/Downloads" & "/notracktemp.txt"
    My.Computer.Network.DownloadFile(url, dirr)
    'System.IO.File.Delete(localDir & "/notracktemp.txt")
End Sub

1 个答案:

答案 0 :(得分:0)

tempStore()必须有一个大小 计算带循环的文件中的行数,然后将其声明为tempStore(i),其中i是行数。这是一个对行进行计数的函数。

    Function countlines()
    Dim count As Integer
    Dim s As New IO.StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "/Downloads" & "/notracktemp.txt", True)
    s.ReadLine()
    s.ReadLine()
    count = 0
    Do Until s.EndOfStream = True
        s.ReadLine()
        count = count + 1
    Loop
    Console.WriteLine(count)
    Return count
    Console.ReadKey()
End Function

然后你做的是:

    Dim count As Integer
    count = countlines()

    Dim tempStore(count) As String