0

I sampled some code from a post around here. I am trying to read a .txt file with LINQ and make the query confirm that a username is indeed inside of it (with an if statement), and then proceed to another if statement that will check if the password is existing, and then extract that information.

The .txt file is formatted like this

<Site>   ||<Email/User>               ||<Password>           
Google                 | UserNameOk123                | Password12312        
Facebook               | overlordvader@msn.com            | 832188981323         
Gmail                  | coolkids22@gmail.com         | password1 
Yahoo    (lol)         | manualentry1@yahoo.com           | shtein998877
Reddit                 | wildsavage2                  | 88ca$                  
reddit.com             | patrickthestar                   | krustykrabs22        
iCloud                 | asdasd@me.com                    | gigihadid8$   

How do I properly work around this code: (doesn't compile)

public void ScanFile( string input)
{
    string[] AllLines = new string[];      
    AllLines = File.ReadAllLines(FILE_PATH);
    Parallel.For(0, AllLines.Length, x =>
    {
        if (from lines in AllLines
            where lines.StartsWith("firstHalfOfUsername")
            && lines.EndsWith("secondHalfOfUsername")
            select line)
        {
        }
    });
}
budi
  • 6,351
  • 10
  • 55
  • 80
S. Nog
  • 61
  • 10
  • Is this application for person use? Or are you storing user's passwords in plain text...? – maccettura Jul 21 '17 at 19:29
  • its personal use, I will encrypt in bin before final product :) – S. Nog Jul 21 '17 at 19:46
  • Encrypt the file? Thats not enough, you need to salt and hash the passwords. – maccettura Jul 21 '17 at 19:47
  • I will look into this and how to do it. It is a project for my internship so I am still wrapping my head around making a good product, I appreciate the tip :) – S. Nog Jul 21 '17 at 19:50
  • There's a bug in the logic. Parallel.For is passing individual lines to your Action<> but you're still searching the entire array for each line. You've gotten good answers to your problem, but I want to add that the Parallel class is definitely advanced stuff and you need to know what you're doing or you're just going to end up with something that isn't correct and is actually slower. Sure, you could check each line in parallel but the overhead would kill performance unless your line processing is significant. If you want it fast then do a better read. – Ray Fischer Jul 22 '17 at 01:34

1 Answers1

0

Instead of using LINQ Query Expressions you could do it with extension methods:

if(AllLines.Any(line=> lines.StartsWith("firstHalfOfUsername") && lines.EndsWith("secondHalfOfUsername")))
{
//...
}

I belive it would help with readability

lukbl
  • 1,763
  • 1
  • 9
  • 13
  • another question, I came to notice that because of File.ReadAllLines the application takes a substantial amount of time to iterate through every line and break it into an array. Is there a better way to make it just read until it finds the specified firsthalf/seconhalf strings? I am thinking of doing a `StreamReader` that returns a Boolean corresponding to whether or not it has found the strings – S. Nog Jul 21 '17 at 20:15
  • 1
    you could use following answer for this: https://stackoverflow.com/a/9643111/5151500 – lukbl Jul 21 '17 at 20:21