What if you have a big text file that has many entries in it you have to get those and save them to List is there any faster way to do that rather than traditional way as in my code
/// <summary>
/// Reads Data from the given path
/// </summary>
/// <param name="path">Path of the File to read</param>
public void Read(string path)
{
try
{
FileStream file = new FileStream(path, FileMode.Open);
StreamReader reader = new StreamReader(file);
string line;
while ((line = reader.ReadLine()) != null)
{
myList.Add(line);
System.Windows.Forms.Application.DoEvents();
}
reader.Close();
file.Close();
}
catch (Exception c)
{
MessageBox.Show(c.Message);
}
}
Faster - probably not, but much shorter? yes:
myList.AddRange(File.ReadAllLines(path));
Also if this is a very big file you shouldn't be doing the loading in the UI thread, just use a background worker - that would save you from calling Application.DoEvents()
on every line and probably will make this somewhat faster than your original approach - although compared to the cost of File I/O the savings will be minimal, but you will have cleaner code.