tebdilikiyafet
tebdilikiyafet

Reputation: 405

Get unknown file content on a form application

There is a file type with extension ".pop". I can open it with notepad. I want to get this file's content line by line on my C# form application. How can I do this?

Upvotes: 1

Views: 565

Answers (2)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186823

If myfile.pop is in fact a text file, I suggest using File.ReadLines (which unlike File.AllReadLines reads the file line by line without loading all the file content into memory):

foreach(String line in File.ReadLines("myfile.pop")) {
  //TODO: put relevant code here
}

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1039418

If it is a text file you could use the File.ReadAllLines method:

string[] lines = File.ReadAllLines("myfile.pop");

And then you could loop through the lines array or access individual lines by index. Note however that this will load the entire file contents in memory which might not be very good if the file is quite big. In this case you could read it line by line using a StreamReader:

using (var stream = File.OpenRead("myfile.pop"))
using (var reader = new StreamReader(stream))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        // do something with the line here
    }
}

In this case only a single line is loaded in memory.

Upvotes: 6

Related Questions