因此,我处于一种怪异的境地,因为这是我的新手,在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行,因此我可以下载并将它们放置在正确的文件夹中。我曾尝试为该字符串创建一个数组,但是会反弹并返回一个错误,指出无法将其从字符串转换为对象。
[就像我在这篇文章的顶部说的那样,我真的是新来的,不知道我实际上在做什么,这使得大部分都是Google的CopyPasta。请帮我一个忙,请为您提供任何帮助ELI5,因为我可能不知道您在说什么。
有很多方法可以满足您的需求。这只是一个入门的例子。它带您完成字符串操作。请参考.net中的String类。 https://docs.microsoft.com/en-us/dotnet/api/system.string?view=netframework-4.8
要获取位置输入,您将需要使用DialogBoxes中的一种构建。请参阅设计模式下的工具箱。
要操作文件,请参阅System.IO中的File类(在代码文件的顶部需要导入)https://docs.microsoft.com/en-us/dotnet/api/system.io.file?view=netframework-4.8
此代码的说明。
StreamReader
公开了.Dipose
方法,因此应将其关闭并处置,以便释放非托管资源。即使有错误,Using...End Using
块也会为您解决这一问题。
我让StreamReader
通过添加到列表中的行来提供数据。该列表传递给解析每一行的方法。我创建了一个类来保存数据。当同时找到源和目标时,将在类构造函数中设置该类的属性,然后将该对象添加到SrcDest
列表中。此列表是一个Form级别的变量,因此您可以在Form的任何位置使用其中的对象。
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