我首先要说的是,我是Visual Basic的新手,并且因为好奇而想要扩展我的知识而搞乱一些元素。我正在创建一个控制台应用程序,我想在其中通过命令提示符执行“登录”应用程序。所有它将是“请输入您的用户名”,然后我想在词表上有一个名单列表,然后是IF和ELSEIF等,以便您继续或不继续。
Module Module1
Sub Main()
Dim txt As String
txt = My.Computer.FileSystem.ReadAllText("C:\wordlist.txt")
MsgBox(txt)
Console.WriteLine("Please enter your username: ")
Dim userName As String = Console.ReadLine()
End Sub
End Module
这就是我到目前为止所有的一切,并在互联网上浏览了一些信息,但我似乎找不到任何信息。如果有人有任何帮助或指导,那将是非常感谢!
此致 斧
答案 0 :(得分:0)
通过ReadAllLines()将文本文件加载到数组中,然后您可以使用Contains()进行检查:
Dim UserNames() As String
UserNames = System.IO.File.ReadAllLines("C:\wordlist.txt")
Console.WriteLine("Please enter your username: ")
Dim userName As String = Console.ReadLine()
If UserNames.Contains(userName) Then
Console.WriteLine("Welcome " & userName & "!")
Else
Console.WriteLine("UserName not found: " & userName)
End If
答案 1 :(得分:0)
你可以试试这个:
Module Module1
Sub Main()
Dim txt As New List (Of String)
txt = ReadFileToList("C:\wordlist.txt")
Console.WriteLine("Please enter your username: ")
Dim userName As String = Console.ReadLine()
If txt.IndexOf(userName) = -1 Then
'Username does not exist
Else
'Username exists
End If
End Sub
Private Function ReadFileToList(ByVal Path As String) As List(Of String)
Dim Reader As New IO.StreamReader(Path)
Dim Result As New List(Of String)
Do Until Reader.EndOfStream
Result.Add(Reader.ReadLine())
Loop
Reader.Close()
Return Result
End Function
End Module