Opening files in Listview box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Asprisa
    New Member
    • Oct 2007
    • 30

    Opening files in Listview box

    Hi Guys,

    I posted on another part of the site and was advised to post here, i am currrently developing a small program, all i want it to do is pick up shortcuts from a directory i specify and list them in a listview box and allow me to click them and open the shortcuts in the default program for the file! So far i am able to list the files i need and i have found some code on the web that will (i hope) allow me to open the files when they are clicked, but when i click the file i get an error saying it cannot start the process as the filename has not been provided, here is my code:

    *************** *************** *************** *************** *************** **********
    *************** *************** *************** *************** *************** **********
    Public Class QuickStart

    Private Sub QuickStart_Load (ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load


    Dim di As New IO.DirectoryInf o("C:\QuickStar t")
    Dim aryFi As IO.FileInfo() = di.GetFiles("*. *")
    Dim fi As IO.FileInfo

    For Each fi In aryFi
    ListView1.Items .Add(ClipExt(fi .Name))
    Next



    End Sub

    Private Function ClipExt(ByVal fname As String) As String
    Dim nPos As String
    nPos = InStrRev(fname, ".")
    If nPos > 0 Then
    fname = mid(fname, 1, nPos - 1)
    End If
    ClipExt = fname
    End Function

    Private Sub ListView1_Selec tedIndexChanged (ByVal sender As System.Object, ByVal e As System.EventArg s) Handles ListView1.Selec tedIndexChanged
    Dim psi As System.Diagnost ics.ProcessStar tInfo = New System.Diagnost ics.ProcessStar tInfo()
    psi.FileName = Me.ListView1.Se lectedItems(0). Name
    psi.UseShellExe cute = True
    psi.ErrorDialog = True
    Try
    System.Diagnost ics.Process.Sta rt(psi)
    Catch ex As Exception
    MessageBox.Show (Me, "Couldn't open the file!\n" + ex.ToString(), "Explorer XTreme")
    Finally
    psi = Nothing
    End Try
    End Sub
    End Class
    *************** *************** *************** *************** *************** **********
    *************** *************** *************** *************** *************** **********

    I am assuming it is something to do with my loop at the begining of the code not passing the file name and location to something like an array? i could be wrong! i am using VB 2005 Express, is anyone able to help?

    Kind Regards

    Jay!
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    look at the code when the selected item is to be opened
    psi.FileName = Me.ListView1.Se lectedItems(0). Name

    in your initial loop where you populate the list, you are cliping the extension, and only using the file name (not path)
    So suppose your file is c:\myDirectory\ myFile.myext
    the list shows myFile
    sending myFile as a filename to open will throw an exception.

    The best thing you could do is to m,aintain an internal list of all files as fileInfo, probably in a List<FileInfo>, and everytime a shortcut has to be opened, search the filename in the List<FileInfo> whose filename matches with what is selected, and supply the full name of that file from the fileInfo object

    Comment

    Working...