在VB.NET中循环遍历文本文件的行

时间:2014-09-08 19:33:42

标签: vb.net loops visual-studio-2008 text

我有一个文本文件,里面有一些文字行。

我想遍历每一行,直到找到我想要的项目*,然后以标签的形式将其显示在屏幕上。

*我正在通过文本框搜索该项目。

也就是说,在sudo:

For i = 0 To number of lines in text file
    If txtsearch.text = row(i).text Then
        lbl1.text = row(i).text
Next i

3 个答案:

答案 0 :(得分:5)

您可以使用File.ReadLines Method遍历整个文件,一次一行。这是一个简单的例子:

    Dim Term As String = "Your term"
    For Each Line As String In File.ReadLines("Your file path")
        If Line.Contains(Term) = True Then
            ' Do something...Print the line
            Exit For
        End If
    Next

答案 1 :(得分:3)

这是一个函数,它会从包含搜索词的行中吐出你的字符串......

  Public Shared Function SearchFile(ByVal strFilePath As String, ByVal strSearchTerm As String) As String
    Dim sr As StreamReader = New StreamReader(strFilePath)
    Dim strLine As String = String.Empty

    Try
        Do While sr.Peek() >= 0
            strLine = String.Empty
            strLine = sr.ReadLine
            If strLine.Contains(strSearchTerm) Then
                sr.Close()
                Exit Do
            End If
        Loop

        Return strLine
    Catch ex As Exception
        Return String.Empty
    End Try
  End Function

要使用此功能,您可以执行此操作...

 Dim strText As String = SearchFile(FileName, SearchTerm)
 If strText <> String.Empty Then
   Label1.Text = strText
 End If

答案 2 :(得分:0)

如果我们想要TEXTFILES将“* .txt”放在“* xml”的位置

,请从目录中循环并获取所有XML文件

Dim Directory As New IO.DirectoryInfo(Path)

function setCookie(cname, cvalue, exdays) {
        var d = new Date();
        d.setTime(d.getTime() + (exdays*24*60*60*1000));
        var expires = "expires="+d.toUTCString();
        document.cookie = cname + "=" + cvalue + "; " + expires;
    }

    function getCookie(cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for(var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') {
                c = c.substring(1);
            }
            if (c.indexOf(name) == 0) {
                return c.substring(name.length, c.length);
            }
        }
        return "";
    }

    function checkCookie() {
        var user = getCookie("username");
        if (user != "") {
            alert("Welcome again " + user);
        } else {
            user = prompt("Please enter your name:", "");
            if (user != "" && user != null) {
                setCookie("username", user, 365);
            }
        }
    }

    getCookie('user_first_visited') || setCookie('user_first_visited', Date.now());

    if (!getCookie('user_popup_triggerred')) {
        var loopDetect = setInterval(function(){
            var TimePast = (Date.now() - getCookie('user_first_visited')) / 1000;
            if( TimePast > 5){
                if (localStorage.getItem('surveyOnce') !== 'true') {
            (function() { 
                alert('Hello World!')
                }
              )();
              localStorage.setItem('surveyOnce','true');
              };
            }
        }, 1000);
    }