首页 > 解决方案 > 如何从 Web 读取文件并选择成对的行进行处理?

问题描述

因此,我遇到了一个奇怪的情况,我将作为前言,因为我是新手,在 VBS 中更有“经验”,并且已经将我的很多脚本迁移到 PowerShell。我正在编写一个 WinForms 程序来读取在线存储的 INI 文件,这样我就可以下载 INI 中列出的更新。在 INI 中,每个可下载的文件都存储在一个文本块中(见下文),我需要遍历每个块,找到 "Source=" 和 "Destination=" 行,从源下载文件并将其放在“目的地=”文件夹。在 WinForm 上,用户必须选择下载位置,所以我需要将“Destination=”附加到用户的输入中。

基于 Web 的 INI 文件内容:

[.Net4.6.1FrameworkFull]
DisplayName=.Net Framework 4.6.1 Full
Source=https://download.microsoft.com/download/E/4/1/E4173890-A24A-4936-9FC9-AF930FE3FA40/NDP461-KB3102436-x86-x64-AllOS-ENU.exe
Destination=Redist\Microsoft .net\4.6.1\Full\NDP461-KB3102436-x86-x64-AllOS-ENU.exe
Size=67681000

[.Net4.6.1FrameworkWeb]
DisplayName=.Net Framework 4.6.1 Web
Source=https://download.microsoft.com/download/3/5/9/35980F81-60F4-4DE3-88FC-8F962B97253B/NDP461-KB3102438-Web.exe
Destination=Redist\Microsoft .net\4.6.1\Web\NDP461-KB3102438-Web.exe
Size=1424328

到目前为止,这是我读取 INI 文件并将其转储到控制台的内容:

Dim inStream As StreamReader
Dim webRequest As WebRequest
Dim webresponse As WebResponse
webRequest = WebRequest.Create(DlSrc.Text)
webresponse = webRequest.GetResponse()
inStream = New StreamReader(webresponse.GetResponseStream())
Console.WriteLine(inStream.ReadToEnd)

我可以毫无问题地将 inStream 的输出发送到控制台,但我不知道如何检查每个块的 Source/Destination 行,以便我可以下载并将它们放入正确的文件夹中。我尝试为字符串创建一个数组,但它只是弹回一个错误,说它不能从字符串转换为对象。

就像我在这篇文章顶部所说的那样,我对此真的很陌生,并且不知道我实际上在做什么,这使得大部分内容都来自谷歌的 CopyPasta。请帮我一个忙,ELI5 你可以提供任何帮助,因为我可能不知道你在说什么。

标签: vb.netwinforms

解决方案


有很多方法可以满足您的需要。这只是一个开始的例子。它会带您完成字符串操作。请参阅 .net 中的 String 类。https://docs.microsoft.com/en-us/dotnet/api/system.string?view=netframework-4.8

要获取位置输入,您将需要使用 DialogBoxes 中的构建之一。在设计模式下查看工具箱。

要操作文件,请参见 System.IO 中的 File 类(代码文件顶部需要 Imports)https://docs.microsoft.com/en-us/dotnet/api/system.io.file?view=netframework -4.8

这段代码的解释。

AStreamReader公开一个.Dipose方法,因此它应该被关闭和处置,以便它可以释放非托管资源。Using...End Using即使出现错误,块也会为您解决这个问题。

StreamReader按添加到列表中的行提供数据。List 被传递给解析每一行的方法。我创建了一个类来保存数据。当找到源和目标时,在类构造函数中设置类的属性,并将对象添加到SrcDest列表中。此列表是表单级别的变量,因此您可以在表单的任何位置使用其中的对象。

Private Sub OPCode()
    Dim lines As New List(Of String)
    Using inStream = New StreamReader(WebResponse.GetResponseStream())
        Dim line As String
        Do
            line = inStream.ReadLine()
            lines.Add(line)
        Loop Until line Is Nothing
    End Using
    FillSourceDestinationList(lines)
End Sub

Public Class SourceDestination
    Public Property Source As String
    Public Property Destination As String

    Public Sub New(s As String, d As String)
        Source = s
        Destination = d
    End Sub
End Class

Private SrcDes As New List(Of SourceDestination)

Private Sub FillSourceDestinationList(sourceDoc As List(Of String))

    Dim sou As String = ""
    Dim des As String = ""
    For Each line In sourceDoc
        If line.StartsWith("Source=") Then
            Dim splitString = line.Split("="c)
            sou = splitString(1)
        End If
        If line.StartsWith("Destination") Then
            Dim splitString = line.Split("="c)
            des = splitString(1)
        End If
        If sou <> "" AndAlso des <> "" Then
            Dim sd As New SourceDestination(sou, des)
            SrcDes.Add(sd)
            sou = ""
            des = ""
        End If
    Next
End Sub

Private Sub UseSourceDestinationList()
    For Each item In SrcDes
        Dim source = item.Source
        Dim destination = item.Destination
        'Use these as needed
    Next
End Sub

推荐阅读