0

我真的是VB的初学者,所以如果你能帮助我,我将不胜感激。我的问题真的很简单。我有一个普通的 .txt 文件(比如说 test.txt)我想在两个特定字符(比如说字符 5 和 6)上的特定行(比如说第 22 行)上进行搜索。如果找到的数字大于 18,则执行 .bat 文件。如果不是什么都不做。

我会很感激一些帮助!

4

1 回答 1

0

这应该为您提供一些您需要的示例:

    Public Sub FindCode()

    Dim tempLines As List(Of String)
    Dim position As Int32


    tempLines = ReadFileLines("c:\filename.txt")

    If tempLines Is Nothing Then
        MessageBox.Show("file not read")
        Return
    End If

    If tempLines.Count < 22 Then
        MessageBox.Show("Line 22 does not exist")
        Return
    End If

    'position of line is one less than we are lookign for since List(Of T) is zero based
    position = 22 - 1

    If tempLines(position).Length < 6 Then
        MessageBox.Show("Line 22 does not have 6 characters")
        Return
    End If

    If IsNumeric(tempLines(position).Substring(4, 2)) = False Then
        'characters 5 and 6 (zero based (4) for 2 characters)
        MessageBox.Show("characters 5-6 are not numeric")
    End If

    If CInt(tempLines(position).Substring(4, 2)) <= 18 Then
        MessageBox.Show("characters 5-6 are less than or equal to 18")
        Return
    End If

    System.Diagnostics.Process.Start("c:\batchfile.bat")


End Sub

Public Function ReadFileLines(p_fileName As String) As List(Of String)
    If System.IO.File.Exists(p_fileName) = False Then
        MessageBox.Show("File does not exist")
        Return Nothing
    End If

    Return System.IO.File.ReadAllLines(p_fileName).ToList()

End Function
于 2013-07-21T14:04:40.963 回答