filtering specific lines in C#
I have searched but did not find a solid solution for my project that works. What I want to do is open up a text file and filter it for only lines that start with "description".
This is only a small sample of whats in my text file:
interface Ethernet1/5
description INFRA:TRUNK:myserver4
switchport mode trunk
switchport trunk native vlan 64
spanning-tree mst pre-standard
spanning-tree guard root
udld aggressive
no shutdown
interface Ethernet1/6
description INFRA:TRUNK:easyserver99
switchport mode trunk
switchport trunk native vlan 99
spanning-tree mst pre-standard
spanning-tree guard root
udld aggressive
no shutdown
This is my code th开发者_如何学运维at I am using right now but it is not getting the job done.
private void scrapeConfig_Click(object sender, EventArgs e)
{
string textline;
string description;
//Open and read file
System.IO.StreamReader objReader;
objReader = new System.IO.StreamReader(Chosen_File);
textBox1.Text = objReader.ReadToEnd();
//Scrape for certain lines
do
{
textline = objReader.ReadLine() + "\r\n";
textBox1.Text = textline;
} while (objReader.Peek() != -1);
//Close
objReader.Close();
}
Thank you for your help in advance!
var lines = File.ReadAllLines(filepath)
.Select(l=>l.Trim())
.Where(l=>l.StartsWith("description"));
textBox1.Text = String.Join(Environment.NewLine, lines);
精彩评论