user7103900 user7103900 - 1 month ago 16
C# Question

Is it possible to filter a TextFile in listview in C#

For example the value in my Textfile

1.Description=DATABASESECRIPTION1
1.name = TEST1
1.age = 18

2.Description=DATABASESECRIPTION2
2.name = TEST1
2.age = 14

3.Description=DATABASESECRIPTION3
3.name = TEST1
3.age = 18


i only wanna see

1.Description=DATABASESECRIPTION1
2.Description=DATABASESECRIPTION2
3.Description=DATABASESECRIPTION3


and i only wanna show the value of description in a textfile how can i do this ?
How can i filter this.

my code

Stream mystream;
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if ((mystream = openFileDialog.OpenFile()) != null)
{
string strfilname= openFileDialog.FileName;
string filetext = File.ReadAllText(strfilname);
ListVIew.Text = filetext;
}

Answer

simply you can check if your line contains "description". if so, take rest of text from "=" char. check this:

  var list = new List<string>();
        var text = File.ReadAllLines("1.txt");
        foreach (var s in text)
        {
            if (s.Contains("Description"))
            {
                var desc = s.Substring(s.IndexOf("=") + 1);
                list.Add(desc);
            }
        }

LINQ:

list.AddRange(from s in text where s.Contains("Description") select s.Substring(s.IndexOf("=") + 1));

here is result: enter image description here