Question Listview: Open Item Path from selected row?

tanero82

New member
Joined
Jun 1, 2009
Messages
2
Programming Experience
Beginner
Hi, anyone could be help me?

I have a simple Windows application, it list all Directories from a specified path in a liestview. My Listview have 2 column. First one show only icons and second one is the name of the Directory. Now i have a context menu "open dir". It should be open the Directory in/with explorer - Doesnt work.

The Problem part, i think, is following event: ToolStripMenuItem1_Click

Here is my Code:
VB.NET:
Imports System
Imports System.IO

Public Class Form1




    'Variablen 
    ' Dim txtangabe As String = txtbox1.Text
    Dim suchordner As New DirectoryInfo("D:\MusikVidz") 'Root Ordner angeben
    Dim unterordner As DirectoryInfo() = suchordner.GetDirectories 'Sub Ordner einlesen in variable
    Dim ordnerinfo As DirectoryInfo 'Einzelnen Ordner in Variable speichern





    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Liste löschen
        ListView1.Items.Clear()

        'suchordner = New DirectoryInfo(InputBox("bitte Verzeichnis angeben"))

        For Each Me.ordnerinfo In unterordner ' Für jedes Ordner welches in der Schleife durchläuft erstelle ein Eintrag in die Listview (2.spalte)
            With ListView1.Items.Add("", 0) 'die 0 = die ID des bildes in der Imagelist
                .SubItems.Add(ordnerinfo.Name) '2te Spalte
            End With
        Next (ordnerinfo)


    End Sub


  
    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click

        Try
            Dim lviTmp As System.Windows.Forms.ListViewItem
            If ListView1.CheckedItems.Count > 1 Then
                lviTmp = ListView1.CheckedItems.Item(1)
                MsgBox(lviTmp.SubItems(1).Text)
                Microsoft.VisualBasic.Shell(lviTmp.SubItems.Add(1).Text)
                System.Diagnostics.Process.Start("explorer.exe", lviTmp.SubItems(1).Text)
            End If
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Exclamation, "Fehler")
        End Try


    End Sub
End Class

thx
 
I ran your code real quick and this is what i noticed:

                'Microsoft.VisualBasic.Shell(lviTmp.SubItems.Add(1).Text)
                System.Diagnostics.Process.Start("explorer.exe", lviTmp.SubItems(1).Text)


opened a explorer just fine but the parameter was wrong and explorer just directed me to the default my documents or such.

lviTmp.SubItems(1).Text returned just the name of the directory, so say for ex the dir is "C:\Program Files",
lviTmp.SubItems(1).Text would return = "Program Files" because when you added the item to the listview you used ".SubItems.Add(ordnerinfo.Name) '2te Spalte"

Which only gets the Name of the directory, if you used the FullName Property it would return "C:\Program Files" which should work. Or you can concat the directory name before you open it. So

                '.SubItems.Add(ordnerinfo.Name) '2te Spalte' Change this line to:
                .SubItems.Add(ordnerinfo.FullName) '2te Spalte ' This


                ''OR
                'System.Diagnostics.Process.Start("explorer.exe", lviTmp.SubItems(1).Text)' Change this line to:
                System.Diagnostics.Process.Start("explorer.exe", suchordner.FullName & lviTmp.SubItems(1).Text) 'this


and it should work... ?
 

Latest posts

Back
Top