Search code examples
c#winformsstreamreader

C# Search for term on a text file and put it in textbox


I've got one .properties file with this content:

# Credenciais da Base de Dados

 host_bd= localshot
#
user_bd=root
#
pass_bd=
#

So what I want is to read this file, and pass (for example) "localhost" to one textbox.

I know I have to search for "host_bd=" and read all line, but how can I pass just the "localhost" to the textbox ?

Edit: What I've tried so far

I can read the entire file and put the content in a textbox (just use streamreader for read all file).

Also have one function that I use for save the textbox values to the txt file, but this function write all file again and substitutes the search term for the replace term (searchterm+somevalue), I've tried to change this function for my purpose but no luck so far...


Solution

  • string line = File.ReadAllLines(filePath).Where(l =>l.Trim().StartsWith("host_bd")).FirstOrDefault();
    string value = line.Split('=')[1].Trim();