Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello

I have a txt file like this:

[srv1]
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

line.Substring()

End If

I dont know how to continue
Posted
Updated 31-Aug-18 0:50am
Comments
Richard MacCutchan 31-Aug-18 6:34am    
You could just iterate (For Each) all the lines and check for the keywords 'user' and 'password'.

 
Share this answer
 
Write a quick settings reader:


vb
converted from c# with Code Converter C# to VB and VB to C# – Telerik[^]
VB
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
            Else

                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
        Next
    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

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


c#
C#
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})");
                        }
                        _settings[sectionName].Add(key,value);
                    }
                }
            }
        }

        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);
            }
        }
    }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900