3
Contributors
8
Replies
33
Views
2 Years
Discussion Span
Last Post by PerplexedB
0

Im am making an application to register for conferences and im using text files to store the user id and password, but there are many users\passwords so to find specific user\passwords im using symbols like * or < but i dont know how to read the file and look for the specific chars

0

Have a look at the code below, and see if that works for you, and if not, let us know why. Thanks and good luck.

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            makeTexfile();
            Boolean ok = checkUser("name1","password1");
            if (ok) {
                Console.WriteLine("Name password pair is valid");
            }
            Console.ReadKey();
        }

        private static bool checkUser(string name,string password)
        {
            string[] lines = System.IO.File.ReadAllLines(@"USER.TXT");
            foreach (string line in lines)
            {
                int positionSeparator = line.IndexOf("-");
                if (line.Substring(0,positionSeparator) == name )
                {
                    if (line.Substring(positionSeparator + 1) == password)
                        return true;
                }
            }
            return false;
        }

        private static void makeTexfile()
        {
            string[] lines = { "name1-password1", "name2-password2", "name3-password3" };
            System.IO.File.WriteAllLines(@"USER.TXT", lines);
        }
    }
}
0

Its a windows form but i'll still try the substring method thanks i will let you know if it finds it.

0

So I tried it and im getting this:

private void btnLogin_Click(object sender, EventArgs e)

 private void btnLogin_Click(object sender, EventArgs e)
        {
            StreamReader Chapter = new StreamReader("Chapters.txt");
            Chapter.ReadLine();
            string[] lines = System.IO.File.ReadAllLines("Chapters.txt");
            foreach (string line in lines)
            {

                int ID = line.IndexOf("-");
                int Pass = line.IndexOf("*");

                if (line.Substring(0,ID) == txtID.Text)
                {
                    if (line.Substring(Pass + 1) == txtPassword.Text)
                    {
                        Chapter_Home Home = new Chapter_Home();
                        Home.ShowDialog();
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Incorrent ID or Password");
                    }
            }

        }        
0

so i tried creating an int variable to int.parse() the txtbox but that didn't work either any help is greatly appreciated

1

PerplexedB: You can't do the following:

int positionSeparator = line.IndexOf("-");

if (line.Substring(0,positionSeparator) == name )
{

It will throw an exception for any line that doesn't contain a "-".
positionSeparator = -1 when a line doesn't contain a "-". line.Substring(0,-1) will throw an exception.

See my post here

Edited by cgeier

0

cgeier, I stand corrected. But since I made my own textfile (as at the time we had no clue of it's structure), I was pretty sure I would not have a problem with that :)

Thanks.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.