VB.NET程序总是读取上次创建的文本文件
正在尝试创建登录表单 我的编码目前是:VB.NET程序总是读取上次创建的文本文件,vb.net,visual-studio,Vb.net,Visual Studio,正在尝试创建登录表单 我的编码目前是: Imports System Imports System.IO Public Class frmLogin Dim username As String Dim password As String Dim fileReader As String Dim folderpath As String Dim files As Integer Dim filepath As String Publ
Imports System
Imports System.IO
Public Class frmLogin
Dim username As String
Dim password As String
Dim fileReader As String
Dim folderpath As String
Dim files As Integer
Dim filepath As String
Public Structure info
Dim U As String
Dim P As String
End Structure
Dim details As info
Private Sub btnlogin_Click(sender As Object, e As EventArgs) Handles btnlogin.Click
If txtusername.Text = details.U And txtpassword.Text = details.P Then
MessageBox.Show("Correct!")
frmmenu.Show()
Me.Hide()
Else
MessageBox.Show("wrong")
txtusername.Clear()
txtpassword.Clear()
End If
End Sub
Private Sub btncreate_Click(sender As Object, e As EventArgs) Handles btncreate.Click
frmcreate.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
files = files + 1
filepath = "C:\Users\TheGlove\Desktop\Alex's Program\loginfile" & files & ".txt"
Dim di As DirectoryInfo = New DirectoryInfo("C:\Users\TheGlove\Desktop\Alex's Program")
folderpath = "C:\Users\TheGlove\Desktop\Alex's Program"
files = System.IO.Directory.GetFiles(folderpath, "*.txt").Count
For Each fi In di.GetFiles()
MsgBox(fi.Name)
Dim FILE = System.IO.File.ReadAllLines("C:\Users\TheGlove\Desktop\Alex's Program\loginfile" & files & ".txt")
Dim myArray As String() = FILE
details.U = myArray(0)
details.P = myArray(1)
Next
End Sub
End Class
当我让按钮1工作时,它将与btnlogin合并,目前它只是一个单独的按钮,用于读取每个文本文件。
当按下每个按钮(按钮1->btnlogin)时,只有最后创建的文本文件是正确的。从外观上看,您的代码确实读取了所有文本文件,但不断用从每个文件检索到的值覆盖
details.u
和details.p
。因此,当循环到达最后一个文件时,这些值就是details
对象中的值
我假设您希望将所有用户名和密码读入一个列表,并根据该列表检查文本框中的详细信息,因此。。您的代码可能与下面的代码类似(请参阅代码注释以了解一些差异的解释)
在我们开始编写代码之前,我可以给你一些提示
首先,始终尝试使用有意义的名称。将您的结构定义为Info
并没有它应有的意义。例如,您最好将其命名为UserInfo
,而不是使用p
和U
,您最好使用密码和用户名
。这可能并不合适太多了,但是当你开始编写更大更复杂的程序,并且必须在6个月后回来更新它们时,info.P
或details.P
没有建议的名称那么有用
第二,正如@ajd所提到的。不要使用神奇的字符串。在代码的开头创建一个定义,可以在整个代码中使用。同样,如果只需更改一次字符串而不是多次,那么维护会更加容易,并减少出错的机会
最后,您定义的一些变量根本没有在代码中使用。同样,在这个级别上,这不是一个主要问题,但是对于大型程序,您可能会得到比您想要的更大的内存占用
Dim username As String
Dim password As String
Dim fileReader As String
Dim folderpath As String = "C:\Users\TheGlove\Desktop\Alex's Program"
Dim files As Integer
Dim filepath As String
Public Structure UserInfo
Dim Name As String
Dim Password As String
End Structure
'Change details to a list of info instead of a single instance
Dim userList As New List(Of UserInfo)
Private Sub Btnlogin_Click(sender As Object, e As EventArgs) Handles btnlogin.Click
'Iterate through the list of details, checking each instance against the textboxes
For Each tempUserInfo As UserInfo In userList
If txtusername.Text = tempUserInfo.Name And txtpassword.Text = tempUserInfo.Password Then
MessageBox.Show("Correct!")
frmmenu.Show()
Me.Hide()
'This is here, because after your form has opened an closed, the loop
'that checks usernames and passwords will continue. The line below exits the loop safely
Exit For
Else
MessageBox.Show("wrong")
txtusername.Clear()
txtpassword.Clear()
End If
Next
End Sub
Private Sub Btncreate_Click(sender As Object, e As EventArgs) Handles btncreate.Click
frmcreate.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'clear the list of user details otherwise, if the files are loaded a second time,
'you'll get the same details added again
userList.Clear()
'This line replaces several lines in your code that searches the folder for files
'marching the search pattern
Dim fileList() As FileInfo = New DirectoryInfo(folderpath).GetFiles("loginfile*.txt")
For Each fi As FileInfo In fileList
MsgBox(fi.Name)
Dim userDetails() As String = System.IO.File.ReadAllLines(fi.FullName)
Dim tempInfo As New UserInfo With {.Name = userDetails(0), .Password = userDetails(1)}
'An expanded version of the above line is
'Dim tempInfo As New info
'tempInfo.U = userDetails(0)
'tempInfo.P = userDetails(1)
userList.Add(tempInfo)
Next
files = fileList.Count
End Sub
使用Dim FILE=System.IO.FILE.ReadAllLines(“C:\Users\TheGlove\Desktop\Alex's Program\loginfile”&files&“.txt”)行,您只读取最后一个文件,因为files变量只是保存文本文件的总数。因此,如果文件夹中有4.txt文件,您将只读取loginfile4.txt。您有一些“神奇字符串”在您的代码中-例如,您设置的文件路径
,然后在可以文件=…
的情况下不要重复使用。避免这种重复可能有助于调试。尤其是在文件夹路径
中使用字符串,然后将其设置为变量!