read a specific line from a txt file in VB .net
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"
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!!
--Adam
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]")
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.
You can then extract the path from the line as per Rubens' or SLaks' answers.
Dim path As String = thatLine.Split("""")(1)
Assuming that the path will never contain quotes, you can use a regular expression:
Dim regex As New Regex(".+=\s*""(.+)""")
Dim path As String = regex.Match(line).Groups(1).Value
Alternatively, you can search for the quotes and extract the part between them using string functions, like this: (This assumes that there will always be exactly two quotes)
Dim pathStart As String = line.IndexOf(""""c) + 1
Dim path As String = line.Substring(pathStart, line.LastIndexOf(""""c) - pathStart)
精彩评论