-2

Given text file which contains the registration data like a database:

[ID]    [Uname] [PW]    [Email] 
0       Aron    asd     [email protected]
1       Aron2   asdd    [email protected]

I have the username and the password input. How would i read only that line in this text file where my uname.Text and password.Text are given?

6
  • Is this a local test? Because if something like this is used in any production environment, then having a text-file with flat-text passwords is pretty bad.. Nov 26, 2015 at 14:53
  • Yes this is a local test.And also i want to try making a local database which will be integrated in the executable file. :)
    – Aaron
    Nov 26, 2015 at 14:57
  • Parse first line of file to check for the column names then retrieve each line and seperate it on the white-space characters then match against your login input OR a regex
    – Neijwiert
    Nov 26, 2015 at 14:59
  • @Aaron Ah good that it's just a test, you scared me there. As for the actual question: you can use File.ReadLines("\path\to\your\file.txt"); to get a list of strings containing each line of the file. How to convert each of the lines into separated strings depends on a few things. Are the four columns shown the only four, and does none of them contain any whitespaces? If so, you could use a white-space split on the line-strings to separate them. Nov 26, 2015 at 15:01
  • It looks like the columns have fixed positions. String.Substring can be used to extract the different parts from the lines. String.IndexOf can be used to find the [ characters on the first line.
    – Marcus
    Nov 26, 2015 at 15:47

3 Answers 3

0

I agree with all the comments above. With the hypothesis that the file is not huge, you can simply load it all in memory and work on it:

//Load your files in a list of strings
IList<string> lines = File.ReadLines("\path\to\your\file.txt");

//Filter the list with only the pattern you want
var pattern = username + "[ ]{1,}" + password;
Regex regex = new Regex(pattern);
IList<string> results = lines.Where(x => regex.IsMatch(x)).ToList();

Here's a .NET fiddler that shows this.

1
  • Wow,I didn't expected it is possible. Thanks.
    – Aaron
    Nov 26, 2015 at 16:13
0

If anyone have this problem too, this is my solve:

int check=0;
if (txt_uname.Text != "")
            {
                check = 0;
                System.IO.StreamReader file = new System.IO.StreamReader(path);
                string[] columnnames = file.ReadLine().Split('\t');
                string newline;
                while ((newline=file.ReadLine()) != null)
                {
                    string[] values = newline.Split('\t');
                    if (check== 0){
                        for (int i = 0; i < values.Length; i++)
                        {
                            if (txt_uname.Text == values[i] && txt_pw.Text == values[i + 1])
                            {
                                Console.WriteLine("User found");
                                check= 1;
                                break;

                            }
                            else
                            {
                                Console.WriteLine("User isn't exists");

                            }
                        }


                    }

                }
0

Try this:

var username = "Aron2";
var password = "asdd";
List<string> matchedValues; // Contains field values of matched line.
var lines = File.ReadLines("input.txt");

foreach (string l in lines)
{
    var values = l.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
 
    if (values.Contains(username) && values.Contains(password))
    {
        matchedValues = values;
        break; // Matching line found. No need to loop further.
    }
}
1
  • Thank you!Maybe this works too,but I solved my problem as Kevin wrote. :))
    – Aaron
    Nov 26, 2015 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.