tags:

views:

526

answers:

3

Hi, 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

A: 
Dim path As String = thatLine.Split("""")(1)
Rubens Farias
what do you mean for thatLine?
Niphoet
This code is very brittle.
SLaks
A: 

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)
SLaks
+1  A: 

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.

itowlson
I got it working, thanks a million.
Niphoet