DoubleClick a ListView Control Selected Item to Open a File

Posted on 2004-11-08
Medium Priority
Last Modified: 2008-02-01

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
Question by:bcunning
  • 2

Expert Comment

ID: 12526957
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"
LVL 86

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 12527040
   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

Author Comment

ID: 12527129
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

Expert Comment

ID: 12527157
*laughs*... I even rewrote that line and didn't notice the quotes... Silly me. :)

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question