-1

我有一个包含以下内容的 .properties 文件:

# Credenciais da Base de Dados

 host_bd= localshot
#
user_bd=root
#
pass_bd=
#

所以我想要的是读取这个文件,并将(例如)“localhost”传递给一个文本框。

我知道我必须搜索“host_bd =”并阅读所有行,但我怎样才能将“localhost”传递给文本框?

编辑:到目前为止我尝试过的

我可以读取整个文件并将内容放入文本框中(只需使用 streamreader 读取所有文件)。

还有一个用于将文本框值保存到 txt 文件的函数,但是此函数会再次写入所有文件并将搜索词替换为替换词(搜索词 + 某个值),我已尝试更改此函数以达到我的目的但到目前为止还没有运气...

4

2 回答 2

1
Regex regex = new Regex(@"^\s*host_bd\s*=\s*(?<host_bd>.*)\s*$", RegexOptions.Multiline);

string fileContent = File.ReadAllText(".properties");

Match m = regex.Match(fileContent);

if (m.Success)
{
    myTextBox.Text = m.Groups["host_bd"].Value;
}
else
{
    myTextBox.Text = "unknown";
}
于 2013-09-26T11:17:23.073 回答
1
string line = File.ReadAllLines(filePath).Where(l =>l.Trim().StartsWith("host_bd")).FirstOrDefault();
string value = line.Split('=')[1].Trim();
于 2013-09-26T11:19:22.687 回答