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

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);
        }
    }
}

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

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");
                    }
            }

        }        

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

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 2 Years Ago by cgeier

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 article has been dead for over six months. Start a new discussion instead.