Mike Tobias

Hi, I am fairly new to C#, i have a project in school for a ATM program. The project comes with a .txt file in my documents folder and it contains usernames and passwords. i have my userface done for the login screen but i do not know how to write the code so that it will check the .txt file in my documents to match up a valid username and password. Any help would be greatly appreciated. I have no previous programming experience and the Visual C# 2005 book does not really tell you much unless you have experience.


Re: Visual C# General password and username

Mike Tobias

i have a project for a ATM program in school. The project comes with a .txt file in My Documents. I have the user interface done for the login form but i do not know how to write the code to check the .txt file for a valid username and password and then grant access if the username and password is correct. I am new to c# and any help would be greatly appreciated.




Re: Visual C# General password and username

paoloTheCool

Can you post the text of the txt file

Thanks,

paoloTheCool





Re: Visual C# General password and username

Steve Py

It depends on what is in the text file.
If the text file has each user name and password on a new line with the user name and password separated by a comma like this:

User1, User1PW
User2, User2PW

then you can use code to read each line from the text file and add it to a data structure suitable to search through in your login dialog. Sprinkle with a generous amount of testing and exception handling.

Dictionary<string, string> userList = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);


StreamReader reader = File.OpenText( "sample.txt" );
while ( !reader.EndOfStream )
{ // Load the values from the text file into the dictionary.
string[] values = reader.ReadLine().Split(',');
userList.Add( values[0].Trim(), values[1].Trim() );
}

This gives you a user list that you can use userList.ContainsKey(userNameTextbox.Text) to determine if the user ID is in your list (case insensitive) then if it exists you can check the password via:
if ( passwordTextbox.Text == userList[userNameTextbox.Text] )
{
// Password matches, case sensitive..
}

or

if ( string.Compare(passwordTextbox.Text, userList[userNameTextbox.Text], true) == 0 )
{
// Password matches, case insensitive..
}






Re: Visual C# General password and username

OmegaMan

There are static methods off of System.IO File Class which will allow you to read the whole contents of a file into a string (File.ReadAllText Method). From there you can process the string for the information needed.





Re: Visual C# General password and username

Mike Tobias

this is what the txt file looks like

Korben Dallas,D001
Jerry Cann,C001
Eric Clapton,C002
Carlos Santana,S001
Elton John,J001

how would i write the code to read these these usernames and passwords to see if there is a match





Re: Visual C# General password and username

Mike Tobias

Korben Dallas,D001
Jerry Cann,C001
Eric Clapton,C002
Carlos Santana,S001
Elton John,J001

this is the way that it is written and the name of the file is Customers.txt





Re: Visual C# General password and username

paoloTheCool

Ok a few things....this code will one only work if the passwords are always 4 digits. If this is will not work, then we will have to make it more complicated. Smile

Code Block

//string text is the customers.txt string..so replace the line below with how you actually read customers.txt

//textBox1 is the username box

//textBox2 is the password box

//all this code goes in a buttonclick handler

string text = "Korben Dallas,D001Jerry Cann,C001Eric Clapton,C002Carlos Santana,S001Elton John,J001";

if (!text.Contains(textBox1.Text) || !text.Contains(textBox2.Text))

{

MessageBox.Show("Either username or password is incorrect.");

}

else

{

int place = text.IndexOf(textBox1.Text);

int startpassword = textBox1.Text.Length + 1;

if (textBox2.Text == text.Substring(place + startpassword, 4))

{

MessageBox.Show("Correct");

}

else

{

MessageBox.Show("Password is incorrect.");

}

}

Thanks,

paoloTheCool

BTW, looks like the moderators marked it as answer, but you should unmark it until it actually is resolved....