Connecting Tech Pros Worldwide Help | Site Map

Search for a shortcut .exe file and displays the it's file path

Newbie
 
Join Date: Aug 2008
Posts: 3
#1: Aug 11 '08
Hi there,

I want the programe to search for the shortcut exe file on the desktop and display the file path of the file. To obtain the file path manually, right-click the shortcut exe file and go to properties and the path is under the row called Target.

Currently the code i wrote cannot display the path. Can you tell me wat is the problem. I m currently using vb.net 2003.

Thanks in advance
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdGet_Path_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdGet_Path.Click
  2.         Dim file_name As String
  3.         file_name = Me.txtShortcutVerif_DesktopName.Text
  4.  
  5.         GetShortcutPathInfo(file_name)
  6.     End Sub
  7.  
  8.     Private Sub GetShortcutPathInfo(ByVal file_name)
  9.         Dim oShell As Shell32.Shell
  10.         Dim oFolder As Shell32.Folder
  11.  
  12.         oShell = New Shell32.Shell
  13.  
  14.         oFolder = oShell.NameSpace(ssfDESKTOP)
  15.  
  16.         'Check if the oFolder is empty and if not empty then 
  17.         If Not oFolder Is Nothing Then
  18.             'Declare the folder items
  19.             Dim oFolderItem As Shell32.FolderItem
  20.             Dim i As Integer
  21.             Dim bFound As Boolean
  22.             For i = 1 To oFolder.Items.Count
  23.  
  24.  
  25.                 If String.Compare(file_name, oFolder.Items.Item(i).Name, True) <> 0 Then
  26.                     'If oFolder.Items.Item(i).Path = file_path Then
  27.                     oFolderItem = oFolder.Items.Item(i)
  28.  
  29.                     Dim dir As Shell32.Folder = oShell.NameSpace(oFolderItem.Path)
  30.                     Dim itm As Shell32.FolderItem = dir.Items().Item(i)
  31.                     Try
  32.                         Me.txtShortcutVerif_GetName.Text = itm.Name
  33.                     Catch ex As NullReferenceException
  34.  
  35.                     End Try
  36.  
  37.                     Debug.WriteLine(oFolder.Items.Item(i).Name)
  38.  
  39.                     bFound = True
  40.                     Exit For
  41.                 Else
  42.                     If bFound = False Then
  43.                         MsgBox("Cannot find the file", MsgBoxStyle.Information)
  44.                     End If
  45.                 End If
  46.  
  47.             Next
  48.  
  49.  
  50.         End If
  51.         oFolder = Nothing
  52.         oShell = Nothing
  53.     End Sub
  54.  
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,158
#2: Aug 11 '08

re: Search for a shortcut .exe file and displays the it's file path


Have you tried setting a breakpoint and stepping though the code?
I would guess that it does not find a match for the shortcut name you request
Newbie
 
Join Date: Aug 2008
Posts: 3
#3: Aug 12 '08

re: Search for a shortcut .exe file and displays the it's file path


Quote:

Originally Posted by Plater

Have you tried setting a breakpoint and stepping though the code?
I would guess that it does not find a match for the shortcut name you request


Yes, i did use the breakpoint n has stepped thourgh it.

This is what is display when i put this in the Command Window:

?ofolder.Items.Item(i)
{System.__ComObject}
[System.__ComObject]: {System.__ComObject}
Application: {System.__ComObject}
GetFolder: {System.__ComObject}
GetLink: <error: an exception of type: {System.InvalidCastException} occurred>
IsBrowsable: False
IsFileSystem: False
IsFolder: True
IsLink: False
ModifyDate: #12/30/1899#
Name: "網路上的芳鄰"
Parent: {System.__ComObject}
Path: "::{208D2C60-3AEA-1069-A2D7-08002B30309D}"
Size: 0
Type: "系統資料夾"

Can you tell me what the problem?
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,158
#4: Aug 12 '08

re: Search for a shortcut .exe file and displays the it's file path


Well I am confused as to why you use COM objects to sift through a directory when there are nice .NET object that will do it for you?
I would use the .NET functions to locate the .lnk file you want and only THEN use the COM object to parse it for your desired location.
Newbie
 
Join Date: Aug 2008
Posts: 3
#5: Aug 13 '08

re: Search for a shortcut .exe file and displays the it's file path


Quote:

Originally Posted by Plater

Well I am confused as to why you use COM objects to sift through a directory when there are nice .NET object that will do it for you?
I would use the .NET functions to locate the .lnk file you want and only THEN use the COM object to parse it for your desired location.

Actually, i m very new to VB.net. So still not really sure if i m doing the right thing. ok. let me try ur way.

Thanks lots
Reply