VB.NET读取文本文件中的某些文本 [英] VB.NET Read Certain text in a text file

查看:178
本文介绍了VB.NET读取文本文件中的某些文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的程序读取文本文件中的某些文本。例如,如果我有一个文本文件,其中包含以下信息。

  acc = blah 
pass = hello

我希望我的vb.net应用程序能够知道帐户变量等于blah,密码变量等于你好。



任何人都可以告诉我如何做到这一点?

谢谢

$

解决方案

这是一小段代码,点击按钮后,


  1. 获取一个输入文件(在这种情况下,我创建了一个名为test.ini的文件)


  2. 使用正则表达式搜索它是否包含任何ACC =或PASS =参数
  3. 然后将它们写入控制台



  4. $ b

    Imports System.IO
    Imports System.Text.RegularExpressions

    Public Class Form1

    Private Sub Button1_Click(ByVal send呃作为System.Object,ByVal e As System.EventArgs)处理Button1.Click
    Dim strFile As String =Test.INI
    Dim sr As New StreamReader(strFile)
    Dim InputString As字符串

    虽然sr.Peek<> -1
    InputString = sr.ReadLine()
    checkIfContains(InputString)
    InputString = String.Empty
    End while
    sr.Close()
    End Sub

    Private Sub checkIfContains(ByVal inputString As String)
    Dim outputFile As String =testOutput.txt
    Dim m As Match
    Dim m2 As Match
    Dim itemPattern As String =acc =(\S +)
    Dim itemPattern2 As String =pass =(\S +)
    $ bm = Regex.Match(inputString,itemPattern ,_
    RegexOptions.IgnoreCase或RegexOptions.Compiled)
    m2 = Regex.Match(inputString,itemPattern2,_
    RegexOptions.IgnoreCase或RegexOptions.Compiled)
    Do While m.Success
    Console.WriteLine(Found account {0},_
    m.Groups(1),m.Groups(1).Index)
    m = m.NextMatch()
    Loop
    Do while m2.Success
    Console.WriteLine(Found password {0},_
    m2.Groups(1),m2.Groups(1).Index)
    m2 = m2.NextMatch()
    Loop
    End Sub

    End Class


    I want my program to read certain text in a text file. For example if I have a text file that contains the following info..

    acc=blah
    pass=hello
    

    I want my vb.net application to get that the account variable is equal to blah, and the password variable is equal to hello.

    Can anyone tell me how to do this?

    Thanks

    解决方案

    Here is a quick little bit of code that, after you click a button, will:

    1. take an input file (in this case I created one called "test.ini")
    2. read in the values as separate lines
    3. do a search, using regular expressions, to see if it contains any "ACC=" or "PASS=" parameters
    4. then write them to the console

    here is the code:

    Imports System.IO
    Imports System.Text.RegularExpressions
    
    Public Class Form1
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strFile As String = "Test.INI"
        Dim sr As New StreamReader(strFile)
        Dim InputString As String
    
        While sr.Peek <> -1
            InputString = sr.ReadLine()
            checkIfContains(InputString)
            InputString = String.Empty
        End While
        sr.Close()
    End Sub
    
    Private Sub checkIfContains(ByVal inputString As String)
        Dim outputFile As String = "testOutput.txt"
        Dim m As Match
        Dim m2 As Match
        Dim itemPattern As String = "acc=(\S+)"
        Dim itemPattern2 As String = "pass=(\S+)"
    
        m = Regex.Match(inputString, itemPattern, _
                        RegexOptions.IgnoreCase Or RegexOptions.Compiled)
        m2 = Regex.Match(inputString, itemPattern2, _
                        RegexOptions.IgnoreCase Or RegexOptions.Compiled)
        Do While m.Success
            Console.WriteLine("Found account {0}", _
                              m.Groups(1), m.Groups(1).Index)
            m = m.NextMatch()
        Loop
        Do While m2.Success
            Console.WriteLine("Found password {0}", _
                              m2.Groups(1), m2.Groups(1).Index)
            m2 = m2.NextMatch()
        Loop
    End Sub
    
    End Class
    

    这篇关于VB.NET读取文本文件中的某些文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆