简体   繁体   中英

CS1929 - Trying to read a file, and skip to the line that contains a certain string and return that line's content

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM