如何从VB.NET中的文本文件中读取特定文本 [英] How do read specific text from text file in VB.NET
问题描述
你好
我有一个这样的txt文件:
[srv1]
label = testserver
enable = 1
protocol = http
device = ipaddress
用户= test1
密码= test2
组= 1
版本= xxxx
hop = 1
audio = 1
我想从上面的行中仅提取test1和test2并将其显示在文本框中。
请问有人能帮帮我吗?
提前致谢
< b>我尝试了什么:
我试过了
Dim Findstring1 = IO.File.ReadAllLines(C:\ test.txt).Reverse(// beacuse此行位于文件test.txt的末尾)
如果lines.Contains(user =)那么
lin e.Substring()
结束如果
我不知道如何继续
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
推荐答案
看一下这篇文章: INI文件永远不会死:.NET中的方法 - Developer.com [ ^ ]。
Have a look at this article: INI Files Will Never Die: How-To in .NET - Developer.com[^].
写一个快速设置读者:
vb
从c#转换为 Code Converter C#转换为VB和VB转换为C# - Telerik [ ^ ]
Write a quick settings reader:
vb
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
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(
找到重复键({key})({sectionName}))
结束 < span class =code-keyword>如果
_settings(sectionName).Add(key,value)
End 如果
结束 如果
下一步
结束 Sub
公共 功能 GetValueOr默认值( Of T)( ByVal 部分 As 字符串, ByVal 键作为 字符串)作为 T
如果 < span class =code-keyword>不 _settings.ContainsKey(section)然后
投掷 新 KeyNotFoundException(section)
结束 如果
如果 不 _settings(section)。 ContainsKey(键)然后
投掷 新 KeyNotFoundException(key)
结束 如果
尝试
Dim 结果作为 T = CType (_ settings(section)(key),T)
返回结果
Catch
返回 没什么
结束 尝试
结束 功能
结束 类
"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(
这篇关于如何从VB.NET中的文本文件中读取特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!