Joey Joey - 1 year ago 72 Question

How do I open a folder using the filename directory?

I have a

which contains directories and file names which would be shown like this
. I would like to open the folder of the selected item using a
which would be like this

If System.IO.File.Exists(ListBox1.SelectedItem) = True Then
End If

but of course that only opens the file instead of the folder, how would I trim off the filename so it would only open the folder?

wouldn't be a solution for me as there would be many different directories/files within this
, so it wouldn't always be the same folder for all the listed files.

Answer Source

Use the System.IO.Path.GetDirectoryName() method. This returns the folder path.

For example:

Dim filePath As String = "C:\something\file.txt"
Dim folderPath As String = Path.GetDirectoryName(filePath)

Returns C:\something. So modify your code to:

If System.IO.File.Exists(ListBox1.SelectedItem) = True Then
End If
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download