/ / VB.NET Il programma legge sempre l'ultimo file di testo creato: vb.net, visual-studio

Il programma VB.NET legge sempre l'ultimo file di testo creato - vb.net, visual-studio

Cercando di creare un modulo di accesso,

La mia codifica è attualmente:

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:UsersTheGloveDesktopAlex"s Programloginfile" & files & ".txt"
Dim di As DirectoryInfo = New DirectoryInfo("C:UsersTheGloveDesktopAlex"s Program")
folderpath = "C:UsersTheGloveDesktopAlex"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:UsersTheGloveDesktopAlex"s Programloginfile" & files & ".txt")
Dim myArray As String() = FILE
details.U = myArray(0)
details.P = myArray(1)

Next
End Sub


End Class

Il pulsante 1 verrà unito a btnlogin quando ottengofunziona e per ora è attualmente solo un pulsante separato per leggere ogni file di testo. Quando si preme ogni pulsante (Pulsante 1 -> btnlogin), solo l'ultimo file di testo creato è corretto.

risposte:

0 per risposta № 1

A quanto pare, il tuo codice legge tutti i file di testo, ma continua a sovrascriverli details.u e details.p con il valore recuperato da ogni file. Quindi, quando il ciclo arriva all'ultimo file, quei valori sono ciò che finisce nel file details oggetto.

Presumo che tu voglia leggere tutto il filenomi utente e password in un elenco e controllare i dettagli nelle caselle di testo rispetto a tale elenco, quindi .. Il codice dovrebbe probabilmente essere qualcosa di simile al codice seguente (vedere i commenti sul codice per una spiegazione di alcune differenze.

Prima di arrivare al codice, possiamo darti un paio di suggerimenti.

In primo luogo, cerca sempre di utilizzare nomi significativi. Definire la tua struttura come Info non è così significativo come potrebbe essere. Ad esempio, sarebbe meglio chiamarlo UserInfo e piuttosto che usare P e U, faresti meglio a usare Password e UserName. Potrebbe non avere molta importanza in questo momento, ma quando inizi a scrivere programmi più grandi e complessi e devi tornare tra 6 mesi per aggiornarli, info.P o details.P non sono così informativi come i nomi suggeriti.

In secondo luogo, come menzionato da @ajd. Non usare stringhe magiche. Crea una definizione all'inizio del tuo codice che può essere utilizzata in tutto. Anche in questo caso rende la manutenzione molto più semplice se devi cambiare una stringa solo una volta invece di più volte e riduce la possibilità di errori.

Infine, molte delle variabili che haidefiniti non sono affatto usati nel codice. Anche in questo caso, a questo livello, non è un grosso problema, ma con programmi di grandi dimensioni, potresti ritrovarti con un footprint di memoria maggiore di quello che desideri.

Dim username As String
Dim password As String
Dim fileReader As String
Dim folderpath As String = "C:UsersTheGloveDesktopAlex"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