Listview: starting process and populating correct value

bfsog

Well-known member
Joined
Apr 21, 2005
Messages
50
Location
UK
Programming Experience
5-10
[resolved] Listview: starting process and populating correct value

This really is not a listview issue, although both stem from using a ListView.

First problem: I am creating a file search tool and I want the files returned to be double clickable. When a ListViewItem is double clicked I want to use Process.Start to open the file.

My ListView looks like this

[File] [Path]
1.jpg c:\images
2.jpg c:\images

And so on.

The problem is, I cannot for the life of me work out how to get the values. Let's say I double click on the first ListViewItem (1.jpg) I would want to open c:\images\1.jpg However I can only seem to access the file column values, never the path column values.

My code to get the file part of the selected row is
VB.NET:
Expand Collapse Copy
        Dim i As Integer = lstViewSearchResults.SelectedIndices.Item(0).ToString
        Dim temp As String = lstViewSearchResults.Items.Item(i).Text
        MessageBox.Show(temp)
        Process.Start(temp)

My second issue is: When actually populating the ListView the Path column has the file path as well, so for example from the above example it would read c:\images\1.jpg

To extract the filename I did
VB.NET:
Expand Collapse Copy
    Function stripPath(ByVal FullPath As String)

        Dim temp As String = ""
        Dim i As Integer
        i = FullPath.LastIndexOf("\")
        temp = FullPath.Substring(i + 1)
        Return temp

    End Function
I cannot fathom how to reverse that, to get everything before the last \

Hope this is clear.
 
Last edited:
The Path class of System.IO have the methods you need; GetFileName, GetDirectoryName, Combine. A roundabout:
VB.NET:
Expand Collapse Copy
Dim filepath As String = "c:\images\1.jpg"
Dim filename As String = IO.Path.GetFileName(filepath)
Dim directorypath As String = IO.Path.GetDirectoryName(filepath)
filepath = IO.Path.Combine(directorypath, filename)
VB.NET:
Expand Collapse Copy
Dim lvi As ListViewItem = lstViewSearchResults.SelectedItems(0)
Dim path As String = IO.Path.Combine(lvi.SubItems(1).Text, lvi.Text)
 
Back
Top