好的,所以我有一个应用程序,当你单击“浏览”按钮时,如果将打开一个文件对话框,在这个对话框中你选择一个DLL,它将该文件发送到列表框。 (OpenFileDialog1代码:)
Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim FileName As String = OpenFileDialog1.FileName.Substring(OpenFileDialog1.FileName.LastIndexOf("\"))
Dim DLLfileName As String = FileName.Replace("\", "")
ListBox1.Items.Add(DLLfileName)
dlls.Add(DLLfileName, OpenFileDialog1.FileName)
Dim FileHistory As String = DLLfileName + "|" + OpenFileDialog1.FileName
StrList.Add(FileHistory)
''Dim Stringtosplit As String() = Openfiledialog1namedata.Split("|")
''Dim Filenametoassign As String = Stringtosplit(0)
''Dim Pathtoassign As String = Stringtosplit(1)
'MsgBox(Filenametoassign)
'MsgBox(Pathtoassign)
End Sub
正如你所看到的那样,我正在分割信息,我的想法是创建一个字符串/字符串集合,每次选择/添加DLL时,它都会保存文件名+路径,所以在重启时我可以调用它重新填充列表框。
我尝试了很多方法并没有太多运气。因为我只能保存DLL的名称。现在我遇到的问题是如何每次将“Filenametoassign | Pathtoassign”保存到单独的字符串,然后在MyBase.Load上调用它们通过此命令重新填充Listbox:
dlls.Add(DLLfileName, OpenFileDialog1.FileName)
(dll of dlls)
Dim dlls As New Dictionary(Of String, String)
答案 0 :(得分:0)
要保存到文本文件,您可以执行以下操作:
Dim sb as new System.Text.StringBuilder
For Each k as String in dlls.Keys
sb.AppendLine(k & "|" & dlls(k))
Next
IO.File.WriteAllText(IO.Path.Combine(Application.StartupPath, "dlls.txt"), sb.ToString)
这将为Key|Value
形式的每个加载的dll创建一行,并将其写入文本文件。
要再次加载它,你会这样做:
If dlls Is Nothing then dlls = New Dictionary(Of String, String)
dlls.Clear()
Dim DllFile As String = IO.Path.Combine(Application.StartupPath, "dlls.txt")
If IO.File.Exists(DllFile) = False Then Exit Sub
Dim LoadedDlls() as String = IO.File.ReadAllLines(DllFile)
For Each s As String in LoadedDlls
Dim Parts() As String = Strings.Split(s, "|", 2)
If Parts.Count <> 2 Then Continue For
dlls.Add(Parts(0), Parts(1))
Next
首先从文件中加载所有行,然后从中生成一个String数组。然后迭代遍历所有行并将|
字符处的每一行拆分为最多两部分。然后它再次添加0部分作为键,将部分1作为值再次添加到字典中。