我想将逗号分隔的文本文件(PracInfo.txt)的内容添加到Form_Load上的Dictionary中。该文件的内容如下:
wlvc,coadmin
mrmd,thadmin
ccoa,oaadmin
bfhl,bfadmin
trty,tradmin
nppp,npadmin
例如,我希望wlvc是键,coadmin是相应的值。我到目前为止的代码如下:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sr As New StreamReader("C:\Programming files\PracInfo.txt")
Dim openWith As New Dictionary(Of String, String)
Do While sr.Peek <> -1
strHold = sr.ReadLine()
' here I would like to use the String.Split method on strHold and then put that into the Dictionary using Dictionary.Add(Key,Value) but not sure how to do this????
Loop
End Sub
End Class
对此事的任何帮助将不胜感激!感谢
答案 0 :(得分:4)
这还没有经过测试,只是psuedocode
Do While sr.Peek <> -1
Dim lineArray As String() = sr.ReadLine().Split(",")
If lineArray.Length = 2 Then
openWith.Add(lineArray(0),lineArray(1))
End If
Loop
答案 1 :(得分:0)
试试这个:
Do While sr.Peek <> -1
Dim line As String() = sr.ReadLine().Split(",")
openWith.Add(line(0), line(1))
Loop
这会将一行读入数组。该数组将在逗号上拆分。然后,您可以分别引用第0和第1个元素作为键和值。
答案 2 :(得分:0)
你可以使用静态方法File.ReadAllLines("filename.txt")
它将返回一个字符串数组,你可以在其上迭代和foreach元素,使用.Split()
分隔逗号分隔的值,然后将它们添加到你的字典。