Dropping Desktop Shortcuts into Treeview using Drag & Drop

Joined
Jun 18, 2006
Messages
23
Programming Experience
3-5
Hello,

I have read tons of MSDN documentation, but I do not fully understand the process of creating drag and drop.

Since there is no shortcut class in .NET, I have to use the COM library.

I need to develop a process where I can drag an existing desktop shortcut onto a treeview and the data/properties such as filename, target, etc from the shortcut is transferred and created as a new node in the treeview.

I finally got to being able to drag and drop text from a .NET textbox to create a node in a treeview with that text.


I need to be able to drag desktop shortcuts onto the treeview, convert the shortcut's properties to variables and then create new node on the treeview.

I envision the psuedocode something like this:

1. drag shortcut over treeview
2. variables grab target, and fullpath properties from the shortcut during the dragdrop
3. create a new node using the values of the variables
4. add the new node to the treeview

The COM reference for the shortcut library is Windows Script Host Object Model

And its Imports statement is:

Imports IWshRuntimeLibrary

I need to know how to do the shortcut's mousedown (to begin the drag) and once it is dropped on the treeview, I need to grab its target and fullname property values.

Please help me!
 
WSH scripting will only let you create shortcuts, not read/modify them. Instead use the Shell library, Add Reference to COM "Microsoft Shell Controls And Automation". Here is code to get file drops and target property if a lnk shortcut. Note that the Shell.Namespace function would only work if path string was terminated with a slash "\". You must also set the TreeView AllowDrop property to True.
VB.NET:
    Private Sub TreeView1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
    Handles TreeView1.DragOver
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.Copy
        End If
    End Sub
 
    Private Sub TreeView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
    Handles TreeView1.DragDrop
        If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
            ' Retrieve list of files and loop through string array
            Dim strFiles() As String = e.Data.GetData(DataFormats.FileDrop)
            Dim tn As TreeNode
            For Each fullPath As String In strFiles
                If IO.Path.GetExtension(fullPath) = ".lnk" Then
                    tn = TreeView1.Nodes.Add(fullPath)
                    tn.Nodes.Add(getShortcutTarget(fullPath))
                End If
            Next
        End If
    End Sub
 
    Function getShortcutTarget(ByVal fullPath As String) As String
        Dim linkHelper As New Shell32.Shell
        Dim pathFolder As String = IO.Path.GetDirectoryName(fullPath)
        Dim linkName As String = IO.Path.GetFileName(fullPath)
        Dim linkFolder As Shell32.Folder = linkHelper.NameSpace(slash(pathFolder))
        If Not linkFolder Is Nothing Then
            Dim linkItem As Shell32.FolderItem = linkFolder.ParseName(linkName)
            If Not linkItem Is Nothing Then
                Dim linkObject As Shell32.ShellLinkObject = CType(linkItem.GetLink, Shell32.ShellLinkObject)
                Return linkObject.Path
            End If
        End If
        Return Nothing
    End Function
 
    Function slash(ByVal path As String) As String
        If path.Substring(path.Length - 1, 1) <> "\" Then
            Return path & "\"
        Else
            Return path
        End If
    End Function
 
IO.Path.GetFileNameWithoutExtension method is good for that.
 
Back
Top