从VB .net中的txt文件读取特定行 [英] read a specific line from a txt file in VB .net

查看:254
本文介绍了从VB .net中的txt文件读取特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VB.net中,我试图从文件中读取特定行. txt文件中该行的示例是:

In VB.net I'm trying to read in a specific line from a file. An example of the line in the txt file is:

[PATH] = "/this/directory/run.exe"

现在,我了解了如何打开文件以在VB中进行读写,但是我需要解析出"(引号)中的路径.任何帮助将不胜感激!

Now I understand how to open the file for reading and writing in VB, but I need to parse out the path in the "" (quotation marks). Any help would be greatly appreciated!!

-亚当

推荐答案

查找该行取决于其区别特征,但是基本上,想法是使用LINQ.例如:

Finding the line depends on what its distinguishing features are, but basically the idea would be to use LINQ. For example:

Dim line As String = File.ReadAllLines(path).FirstOrDefault(Function (s As String) s.StartsWith("[PATH]")

这将使您以"[PATH]"开头的第一行.如果您需要更好的区分度,可以使用更复杂的匹配项,例如正则表达式.

This gets you the first line that begins with "[PATH]". If you need better discrimination you could use a more sophisticated match such as a regex.

然后您可以根据鲁本斯(Rubens)或斯拉克斯(SLaks)的答案从行中提取路径.

You can then extract the path from the line as per Rubens' or SLaks' answers.

这篇关于从VB .net中的txt文件读取特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆