Home > other >  CS1929 - Trying to read a file, and skip to the line that contains a certain string and return that
CS1929 - Trying to read a file, and skip to the line that contains a certain string and return that

Time:11-24

The error occurs in the SkipWhile method, in the second "line" keyword. Displays the following log

CS1929 "char" does not contain a definition for "Contains" and the best extension method overload "Queryable.Contains(IQueryable, string)" requires a receiver of type "IQueryable"

    public static void loginEmployee(string user, string password)
    {
        string filename = "servidores_cadastrados_db_backup.txt";
        string path = AppConfig.appconfig.defaultPath;
        path = Path.Combine(path, filename);

        if (File.ReadAllText(path).Contains(user))
        {

            employee.Registration = File.ReadAllText(path).SkipWhile(line => !line.Contains(user))
            .TakeWhile(line => !line.Contains(user));
        }
        else
        {
            MessageBox.Show("Invalid User or Password..", "Invalid Login", MessageBoxButtons.OK,
                MessageBoxIcon.Error);
        }
    }

CodePudding user response:

You are using File.ReadAllText and then using SkipWhile on that which will return each character of the text. What you should be using is File.ReadLines and iterating over the lines.

employee.Registration = File.ReadLines(path).SkipWhile(line => !line.Contains(user))
        .TakeWhile(line => !line.Contains(user));

Also, I think your .TakeWhile(line => !line.Contains(user) is wrong, shouldn't it be trying to match on the user instead of not match?

employee.Registration = File.ReadLines(path).SkipWhile(line => !line.Contains(user))
        .TakeWhile(line => line.Contains(user));

CodePudding user response:

I guess skip while doesn't work with Characters. maybe try dealing with each one as a string and seeing if that works? although I dont think this is best practice, I just dont really know what you are trying to achieve

File.ReadAllText(path).SkipWhile(line => !line.ToString().Contains(user)).TakeWhile(line => !line.ToString().Contains(user));
  •  Tags:  
  • c#
  • Related