好的,所以我在写一个文本文件,从My.Settings中检索值时遇到了一些麻烦。代码如下。问题是,当我检查文本文件时,它只有第一行(在代码中只是一个简单的字符串)。
Dim settingsfile As String = Directory.GetCurrentDirectory & "\RapidSend_Settings.txt"
Dim objWriter As New System.IO.StreamWriter(settingsfile, False)
objWriter.WriteLine("This is the introductory line")
For Each strEmail As String In My.Settings.User_Emails 'Specialized.StringCollection
objWriter.WriteLine("Email " & strEmail)
Next
For Each strHostName As String In My.Settings.User_HostName ' Specialized.StringCollection
objWriter.WriteLine("Host " & strHostName)
Next
objWriter.WriteLine("End Of File")
objWriter.Close()
这是代码后文本文件的样子:
This is the introductory line
End Of File
我的问题是,是否有人知道问题是什么,或者是否有另一种方法可以做到这一点。 顺便说一下,所有的My.Settings都是Specialized.StringCollection,所以它们包含很多字符串。
答案 0 :(得分:1)
由于您不再按预期方式使用“设置”,因此最好自行创建自己的容器并自行管理:
Friend settingsList As New List(Of String)
这将创建一个可以添加数据的全局字符串集合。它可以是公共的或朋友。还有其他方法可以做到这一点,但这是最简单的方法。也为主机创建一个。
添加它们很简单:
settingsList.Add(some_STRING_variable)
然后保存在App关机:
' get file name to hold the data
Dim file As String
file = System.IO.Path.Combine(Environment.GetFolderPath( _
Environment.SpecialFolder.ApplicationData), "myproduct", "settings.txt")
' result will be Users\Appdata.... where data is supposed to go
' open a stream
Using fs As New IO.StreamWriter(file, IO.FileMode.CreateNew)
Dim n As Integer = 1
' loop thru List contents
For Each s As String In settingsList
' ToDo: edit format to fit your needs
fs.WriteLine(String.Format("{0} = {1}", n.ToString, s))
n += 1
Next
End Using ' close and properly dispose of stream