Avatar of RobertoFreemano
RobertoFreemano
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Open File Dir from Listview (VB.NET)

Hi Experts,

I'm trying to load filenames of apps from a dir into a LV and when click... I want it to open the dir... not the file.

I used this code to load filenames in DIr
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        For Each fileName As String In IO.Directory.GetFiles("\\fs1gbn1fs005\uu-it-ops-service-desk-only$\Software")
            '  ImageList1.Images.Add(Icon.ExtractAssociatedIcon(fileName))
            ListView1.Items.Add(IO.Path.GetFileName(fileName), ImageList1.Images.Count - 1)
        Next
    End Sub

Open in new window

THIS WORKS... but when clicking on the item in LV..
Private Sub ListView1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDoubleClick
For Each file As ListViewItem In ListView1.Items
Dim filePath As String = file.SubItems(0).Text & "\" & file.Text
If file.Selected = True Then
Process.Start(filePath)
End If
Next
End Sub

Open in new window

This bit does work... & i know its for launching the app... I want to open the dir.
Thanks,
Roberto
Programming Languages-Other.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
RobertoFreemano

8/22/2022 - Mon
SOLUTION
David L. Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Mike Tomlinson

Get the folder from the path using System.IO.Path.GetDirectoryName():
        Dim filePath As String = "C:\Users\Mike\Documents\SomeFile.txt"
        Dim folderPath As String = System.IO.Path.GetDirectoryName(filePath)
        Process.Start(folderPath)

Open in new window

RobertoFreemano

ASKER
Hi sl8rz,

I've constructed my code as follows:
 
  Dim filePath As String = "C:\Users\bob\Documents\New Folder"
    Dim folderPath As String = System.IO.Path.GetDirectoryName(filePath)

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        For Each fileName As String In IO.Directory.GetFiles("C:\Users\bob\Documents\New Folder")
            ListView1.Items.Add(IO.Path.GetFileName(fileName))
        Next
        Label1.Text = folderPath
    End Sub

    Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        Process.Start("explorer.exe", filePath)
        Label1.Text = folderPath
    End Sub
End Class

Open in new window

It loads files from specified DIR to LV,,, if I click 1 item, it opens 2 explorer windows.
RobertoFreemano

ASKER
Hi Mike,

I added your suggested - did i put this in right place?????
 Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        Dim filePath As String = "C:\Users\bob\Documents\New Folder"
        Dim folderPath As String = System.IO.Path.GetDirectoryName(filePath)
        Process.Start(folderPath)
    End Sub

Open in new window

It opens folder in explorer, but only as far as Users dir
:(
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Mike Tomlinson

Well...your "filePath" variable ALREADY contains a FOLDER in it, so using GetDirectoryName() is not necessary (it's removing the last part because it assumes it's a filename).

You'd just do:

    Dim filePath As String = "C:\Users\bob\Documents\New Folder"
    Process.Start(filePath)

My code was based off your second snippet, which had a full path to a file:

    Dim filePath As String = file.SubItems(0).Text & "\" & file.Text

Thus my code to remove the filename and open just the folder (as requested).
RobertoFreemano

ASKER
Hi Mike,

My ciode now as follows: (changed the path to show other dirs)
 Dim filePath As String = "C:\Users\bob\Documents\Battlefield 2"

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        ListView1.Items.Add(IO.Path.GetFileName(filePath))
    End Sub

    Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        Process.Start(filePath)
    End Sub

Open in new window

It lists 1 folder "Battlefield 2", but I know there are 3 folders in here... can i just change it to show folders in 'Battlefield 2' folder....
ASKER CERTIFIED SOLUTION
Mike Tomlinson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
RobertoFreemano

ASKER
Bingo!

Thanks Mike & sl8rz.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
RobertoFreemano

ASKER
Hi Mike,

Just one minor bit (if possible)...sorry!
Private Sub ListView1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDoubleClick
        Process.Start(filePath)
    End Sub

Open in new window

When i click selected item (dir name)... is it possible to open the selected dir name from the LV item... rather then filePath on any selected item?

Thanks,
Roberto
David L. Hansen

Welcome
Mike Tomlinson

Try something like...
    Private Sub ListView1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDoubleClick
        Dim lvi As ListViewItem = ListView1.GetItemAt(e.X, e.Y)
        If Not IsNothing(lvi) Then
            Dim folder As String = lvi.Text
            Dim subFolder As String = System.IO.Path.Combine(filePath, folder)
            Process.Start(subFolder)
        End If
    End Sub

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
RobertoFreemano

ASKER
Thanks Mike, Perfect!!!!!