Reputation: 31
Hello guys I want to make a Log In Form with dates from a text file and here's my code and I don't know why it doesn't work and also it doesn't show me any errors :\
{
string UserToSearch = textBox10.Text;
string PasswordToSearch = textBox11.Text;
string[] readText = File.ReadAllLines("LogIn.txt");
try
{
for (int i = 0; i < readText.Length; i++)
{
if (readText[i] == UserToSearch && readText[i + 1] == PasswordToSearch)
{
MessageBox.Show("You found it");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
and in my LogIn.txt file is Horatiu pas1 and Ana pas2
any kind of help would be great. Thank you ^^
Upvotes: 0
Views: 68
Reputation: 186813
It seems that you have a format problem: if actual data looks like
Horatiu pas1
Ana pas2
...
MyLogin MyPassword
User 123
Me MyTopSecretPassword
Scott Tiger
(please, notice that both login and password are in the same line) you have to Split
each line. Another suggestion is to use Linq instead of loop:
var found = File
.ReadLines("LogIn.txt")
.Select(line => line.Split(' '))
.Any(items => items[0] == UserToSearch && items[1] == PasswordToSearch);
if (found)
MessageBox.Show("You found it");
Upvotes: 1