我已经搜索过了,但没有为我的项目找到一个可靠的解决方案。我想要做的是打开一个文本文件,并只过滤以"description“开头的行。
这只是我的文本文件中的一个小示例:
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这是我现在正在使用的代码,但它不能完成工作。
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();
}提前感谢您的帮助!
发布于 2011-09-29 09:17:37
var lines = File.ReadAllLines(filepath)
.Select(l=>l.Trim())
.Where(l=>l.StartsWith("description"));
textBox1.Text = String.Join(Environment.NewLine, lines);https://stackoverflow.com/questions/7591401
复制相似问题