如何使用VB.NET过滤TXT文件?

时间:2011-01-05 09:36:58

标签: vb.net filtering

我有一个txt文件,显示AutoCAD返回的以下信息:

; IAcadToolbar: An AutoCAD toolbar

; Property values:

;   Application (RO) = #<VLA-OBJECT IAcadApplication 00d591b4>

;   Count (RO) = 19

;   DockStatus (RO) = 4

;   FloatingRows = 5

;   Height (RO) = AutoCAD: The toolbar is invisible. Please make it visible

;   HelpString = "Draw Toolbar\n    "

;   LargeButtons (RO) = 0

;   left = 1310

;   Name = "Draw"

;   Parent (RO) = #<VLA-OBJECT IAcadToolbars 224a6b04>

;   TagString (RO) = "ID_TbDraw"

;   top = 646

;   Visible = 0

;   Width (RO) = AutoCAD: The toolbar is invisible. Please make it visible

; Methods supported:

;   AddSeparator (1)

;   AddToolbarButton (5)

;   Delete ()

;   Dock (1)

;   Float (3)

;   Item (1)

问题是我需要过滤它,所以我只剩下:

;   DockStatus (RO) = 4

;   left = 1310

;   Name = "Draw"

;   top = 646

;   Visible = 0

它们也必须保持相同的顺序:

; DockStatus (RO) = 4始终排在最前面 ; left = 1310等等,所有其他信息都可能被丢弃。

任何人都知道如何在VB.NET中做到这一点?

3 个答案:

答案 0 :(得分:3)

Imports System.IO
Imports System.Text

Dim output = New StringBuilder()

' Read the lines from FileName into an array of strings. '
Dim input = File.ReadAllLines(FileName)

For Each line in input

    If line.StartsWith(";   DockStatus (RO) = ") OrElse
       line.StartsWith(";   left = ") OrElse
       line.StartsWith(";   Name = ") OrElse
       line.StartsWith(";   top = ") OrElse
       line.StartsWith(";   Visible = ") Then

       output.AppendLine(line)

    End If

Next

答案 1 :(得分:1)

好的,假设你有一个你逐行阅读的文件,你不能简单地做这样的事吗? (仅限伪代码)

string result;
foreach line in linesFromFile
{
    if(line.StartsWith("; DockStatus") or 
       line.StartsWith("; Name = "))
    {
        result += line;
    }
}

我所呈现的并不漂亮,但可能会让你开始。

答案 2 :(得分:0)

我建议将所有值都放在字符串中,然后使用拆分器 然后过滤掉你需要的值 并按照您的需要对它们进行排序  然后根据输出格式的需要转换它们