Click here to Skip to main content
14,241,305 members
Rate this:
Please Sign up or sign in to vote.

I have a txt file like this:

label = testserver
enable = 1
protocol = http
device = ipaddress
user = test1
password = test2
group = 1
version = xxxx
hop = 1
audio = 1

I want to extract only test1 and test2 from the lines above and display them in textbox.

Could some one help me please?

Thanks in advance

What I have tried:

I have tried

Dim Findstring1 = IO.File.ReadAllLines(C:\test.txt).Reverse (//beacuse this line are at the end of the file test.txt)

If lines.Contains("user = ") Then


End If

I dont know how to continue
Updated 31-Aug-18 0:50am
Richard MacCutchan 31-Aug-18 6:34am
You could just iterate (For Each) all the lines and check for the keywords 'user' and 'password'.
Rate this:
Please Sign up or sign in to vote.

Solution 1

Rate this:
Please Sign up or sign in to vote.

Solution 2

Write a quick settings reader:

converted from c# with Code Converter C# to VB and VB to C# – Telerik[^]
Public Class Settings
    Private ReadOnly _sectionNameRegex As Regex = New Regex("(?<=\[).*(?=\])")
    Private ReadOnly _settingRegex As Regex = New Regex("(?<key>\w+)[^\w]=[^\w](?<value>\w+)")
    Private ReadOnly _settings As Dictionary(Of String, Dictionary(Of String, Object))

    Public Sub New(ByVal settingsFilePath As String)
        Dim settings = File.ReadLines(settingsFilePath)
        Dim sectionName = "default"

        For Each setting As String In settings

            If _sectionNameRegex.IsMatch(setting) Then
                sectionName = _sectionNameRegex.Match(setting).Value

                If Not _settings.ContainsKey(sectionName) Then
                    _settings.Add(sectionName, New Dictionary(Of String, Object)())
                End If

                If _settingRegex.IsMatch(setting) Then
                    Dim match = _settingRegex.Match(setting)
                    Dim key = match.Groups("key").Value
                    Dim value = match.Groups("value").Value

                    If _settings(sectionName).ContainsKey(key) Then
                        Throw New InvalidOperationException($"Duplicate key ({key}) found ({sectionName})")
                    End If

                    _settings(sectionName).Add(key, value)
                End If
            End If
    End Sub

    Public Function GetValueOrDefault(Of T)(ByVal section As String, ByVal key As String) As T
        If Not _settings.ContainsKey(section) Then
            Throw New KeyNotFoundException(section)
        End If

        If Not _settings(section).ContainsKey(key) Then
            Throw New KeyNotFoundException(key)
        End If

            Dim result As T = CType(_settings(section)(key), T)
            Return result
            Return Nothing
        End Try
    End Function
End Class

public class Settings
        private readonly Regex _sectionNameRegex = new Regex(@"(?<=\[).*(?=\])");
        private readonly Regex _settingRegex = new Regex(@"(?<key>\w+)[^\w]=[^\w](?<value>\w+)");
        private readonly Dictionary<string, Dictionary<string, object>> _settings;
        public Settings(string settingsFilePath){

            var settings = File.ReadLines(settingsFilePath);
            var sectionName = "default";
            foreach (string setting in settings){

                if (_sectionNameRegex.IsMatch(setting)) {

                    sectionName = _sectionNameRegex.Match(setting).Value;
                    if (!_settings.ContainsKey(sectionName)) {

                        _settings.Add(sectionName, new Dictionary<string, object>());
                else {
                    if (_settingRegex.IsMatch(setting)) {

                        var match = _settingRegex.Match(setting);
                        var key = match.Groups["key"].Value;
                        var value = match.Groups["value"].Value;
                        if (_settings[sectionName].ContainsKey(key)) {
                            throw new InvalidOperationException($"Duplicate key ({key}) found ({sectionName})");

        public T GetValueOrDefault<T>(string section, string key) {
            if (!_settings.ContainsKey(section)) {
                throw new KeyNotFoundException(section);

            if (!_settings[section].ContainsKey(key)){
                throw new KeyNotFoundException(key);

            try {
                T result = (T) _settings[section][key];
                return result;
            catch {
                return default(T);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100