【发布时间】:2014-08-13 22:38:40
【问题描述】:
我正在开发一个计时器程序,它允许用户为计算机上的每个用户帐户设置一个计时器。我在将设置写入文本文件并从中读取时遇到了一些问题。我想知道是否可以以这种方式编写它-->用户名;允许时间;持续登录;剩余时间;
另外,我也无法从文本文件中读取。首先,我不知道如何确定所选用户是否在文件中。表单那里,如果用户在文件中列出,如何访问该行的其余部分,例如只获取该用户的 allowedTime 或剩余时间?
我尝试了几种方法,但如果这有意义的话,我就是无法让它像我想象的那样去做。 这是到目前为止的代码:
Public Sub saveUserSetting(ByVal time As Integer)
Dim hash As HashSet(Of String) = New HashSet(Of String)(File.ReadAllLines("Settings.txt"))
Using w As StreamWriter = File.AppendText("Settings.txt")
If Not hash.Contains(selectedUserName.ToString()) Then
w.Write(selectedUserName + "; ")
w.Write(CStr(time) + "; ")
w.WriteLine(DateTime.Now.ToLongDateString() + "; ")
Else
w.Write(CStr(time) + "; ")
w.WriteLine(DateTime.Now.ToLongDateString() + "; ")
End If
End Using
End Sub
Public Sub readUserSettings()
Dim currentUser As String = GetUserName()
Dim r As List(Of String) = New List(Of String)(System.IO.File.ReadLines("Settings.txt"))
'For Each i = 0 to r.lenght - 1
'Next
'check to see if the current user is in the file
MessageBox.Show(r(0).ToString())
If r.Contains(selectedUserName) Then
MessageBox.Show(selectedUserName + " is in the file.")
'Dim allowedTime As Integer
Else
MessageBox.Show("the user is not in the file.")
End If
'if the name is in the file then
'get the allowed time and the date
'if the date is older than the current date return the allowed time
'if the date = the current date then check thhe remaning time and return that
'if the date is ahead of the current date return the reamining and display a messgae that the current date needs to be updated.
End Sub
编辑:我只是想确定我是否正确地进行了序列化,并且对于反序列化也是如此。 这是我到目前为止得到的:
Friend userList As New List(Of Users)
Public Sub saveUserSetting()
Using fs As New System.IO.FileStream("Settings.xml", IO.FileMode.OpenOrCreate)
Dim bf As New BinaryFormatter
bf.Serialize(fs, userList)
End Using
End Sub
Public Sub readUserSettings()
Dim currentUser As String = GetUserName()
Dim useList As New List(Of Users)
Using fs As New System.IO.FileStream("Settings.xml", IO.FileMode.OpenOrCreate)
Dim bf As New BinaryFormatter
useList = bf.Deserialize(fs)
End Using
MessageBox.Show(useList(0).ToString)
End Sub
<Serializable>
Class Users
Public userName As String
Public Property allowedTime As Integer
Public Property lastLoggedInDate As String
Public Property remainingTime As Integer
Public Overrides Function ToString() As String
Return String.Format("{0} ({1}, {2}, {3})", userName, allowedTime, lastLoggedInDate, remainingTime)
End Function
End Class
编辑 2: 我对 try/catch 不太熟悉,但这样可以代替吗?
Public Sub readUserSettings()
If System.IO.File.Exists("Settings") Then
Using fs As New System.IO.FileStream("Settings", FileMode.Open, FileAccess.Read)
Dim bf As New BinaryFormatter
userList = bf.Deserialize(fs)
End Using
Else
MessageBox.Show("The setting file doesn't exists.")
End If
End Sub
【问题讨论】:
-
VB 内置了一个完整的
Settings东西。如果您有多个用户,但每个条目都有几条数据,那么序列化List(of myClass)可能更容易 -
我以前使用过
Settings,但它变得有点“笨拙”,有很多用户,每个用户都有几个条目,所以我认为读写文本文件会更干净。我将如何使用List(of myClass)?你能举个小例子吗? -
here is an example 类将相关数据保存在一起比数组更好,您可以通过序列化程序非常轻松地保存/重新加载。
-
我尝试了序列化方法,我想知道如果我做得正确,是否可以获得一些反馈,特别是对于反序列化方法。提前致谢。
-
使用 app.config,.NET 内置了对它们的读写支持。