Answer the question
In order to leave comments, you need to log in
How to properly split lines?
There is a code:
string[] data = File.ReadAllText(OFD.FileName).Split( ':' );
for (int i = 0; i < data.Length - 1; i+=2)
{
logins.Add(data[i]);
pass.Add(data[i+1]);
}
Answer the question
In order to leave comments, you need to log in
It is difficult to say where NRE comes from, as the code, although scary, is working.
An error can occur if OFD, OFD.FileName, logins, or pass is null.
A little advice on how to improve the code:
1. We make a separate type for the login-password pair, and use one list for this
2. We read the file not entirely, but line by line.
var path = "...";
var lines = await File.ReadAllLinesAsync(path);
var allCredentials = lines.Select(Credentials.Parse).ToList();
public record Credentials(string Username, string Password) {
public static Credentials Parse(string line) {
var parts = line.Split(':');
return new(parts[0], parts[1]);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question