我正在将文件中的所有文本读取到一个数组中。我正在执行一条if语句来比较我从特定数组中收集的字符串。但不识字。我使用debug,它直接跳到下一个if语句。
下面是我的代码:
Public Sub CheckConfig()
Dim data1 = My.Computer.FileSystem.ReadAllText("C:\DataCollection\Config.cfg")
'Read All Text
Dim quoteArray As New ArrayList
FileOpen(1, "C:\DataCollection-PPTC\Config.cfg", OpenMode.Input)
Do While Not EOF(1)
quoteArray.Add(LineInput(1))
Loop
FileClose(1)
'Hardward Selection
HardwareSelectionLocation = InStr(quoteArray(1), "=")
HardwareSelection = Mid(quoteArray(1), HardwareSelectionLocation + 1)
If HardwareSelection = "Modbus TCP" Then
HaiwellCPUPort = True
lblConnectionType.Text = "Modbus TCP"
Else
HaiwellCPUPort = False
End If
If HardwareSelection = "RS232 Module" Then
HaiwellRS232Module = True
lblConnectionType.Text = "RS232 Module"
Else
HaiwellRS232Module = False
End If
End Sub配置文件中的数据:
#Hardware Selection :Modbus TCP or Modbus RS232 or RS232 Module or Wireless Module or USB Port
Hardware Selection = Modbus RS232发布于 2020-06-15 14:46:32
我们可以让你的配置读取代码变得更简单、更简单:
Public Sub CheckConfig()
Dim lines = IO.File.ReadAllLines("C:\DataCollection-PPTC\Config.cfg")
For Each line In lines
Dim bits = line.Split({"="c},2) 'Split to at most 2: "X=Y=Z" -> "X","Y=Z"
If bits(0).StartsWith("Hardware Selection") Then
lblConnectionType.Text = bits(1).TrimStart()
HaiwellCPUPort = bits(1).Contains("Modbus TCP")
HaiwellRS232Module = bits(1).Contains("RS232 Module")
End If
'To support other config lines copypaste the block above and change the
'strings inside StartsWith/Contains, the lblConnectionType labels and the HaiwellXx variables as appropriate
Next line
End Sub发布于 2020-06-15 12:18:44
使用这一行HardwareSelectionLocation = InStr(quoteArray(1), "="),您将取出"=“符号后面的数据,但根据您的示例,
Hardware Selection = Modbus RS232在"=“符号后面似乎有一个空格。只需检查预期数据之前或之后的空格。
https://stackoverflow.com/questions/62374436
复制相似问题