DoubleClick a ListView Control Selected Item to Open a File


I have a listview control that lists some files.  I want to be able to doubleclick on the files and have them open.

The code I am trying to use is:

    Private Sub listviewLocalFile_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles listviewLocalFile.DoubleClick

        RunProgram.StartInfo.FileName = "listviewLocalFile.SelectedItems(0).Text" 'SET THE FILE TO OPEN
        RunProgram.StartInfo.Verb = "OPEN" 'SET WHAT TO DO TO THE FILE
        RunProgram.Start() 'RUN THE FILE

    End Sub

When that code runs, I get an, "An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in system.dll.  Additional information: The system cannot find the file specified" error.  An example of what listviewLocalFile.SelectedItems(0).Text contains would be C:\test.txt.

Ben Cunningham
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If you run through with a debugger, can you see what the value of "listviewLocalFile.SelectedItems(0).Text" is?

Chances are it's just the file name, and not the full file path.

You may need to prepend the path information to the string...

PathWeAreCurrentlyViewing = "C:\Path\To\Selected\File\"  ' Note trailing backslash...

RunProgram.StartInfo.FileName = PathWeAreCurrentlyViewing & "listviewLocalFile.SelectedItems(0).Text"
Mike TomlinsonMiddle School Assistant TeacherCommented:
   Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
        Dim lvi As ListViewItem = ListView1.SelectedItems(0)
        If Not (lvi Is Nothing) Then
                Dim RunProgram As New Process
                RunProgram.StartInfo.FileName = lvi.Text
                RunProgram.StartInfo.Verb = "OPEN"
            Catch ex As Exception
                MessageBox.Show(lvi.Text & vbCrLf & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bcunningAuthor Commented:
Thanks Idle Mind!

The problem was the quotes in my line:

RunProgram.StartInfo.FileName = "listviewLocalFile.SelectedItems(0).Text"

Once I removed the quotes, it worked great!

Ben Cunningham
*laughs*... I even rewrote that line and didn't notice the quotes... Silly me. :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.