Get selected files from the desktop

jenCarlos 141 Reputation points
2020-12-01T17:29:03.407+00:00

Hello Programmers ...

Is there a way to get the dynamically selected file or folder paths from the windows 7 desktop?

And the result, I would like to show them in a ListBox1 control.

Yes, it's an example, it would be great.

I await your prompt reply.

Thanks a lot.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,555 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 39,806 Reputation points
    2020-12-02T02:46:44.553+00:00

    On Windows 7 -

    44219-desktop.png

    Code behind Windows Form application -

    Public Class Form1  
        Private DesktopFolderView As Shell32.ShellFolderView  
      
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
            ListBox1.Items.Clear()  
            Dim selItems As Shell32.FolderItems = DesktopFolderView.SelectedItems()  
            For Each fitem As Shell32.FolderItem In selItems  
                ListBox1.Items.Add(fitem.Name)  
            Next  
        End Sub  
      
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click  
            ListBox1.Items.Clear()  
            Dim selItems As Shell32.FolderItems = DesktopFolderView.Folder.Items  
            For Each fitem As Shell32.FolderItem In selItems  
                ListBox1.Items.Add(fitem.Name)  
            Next  
      
        End Sub  
      
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            Dim oShell As Shell32.Shell  
            Dim oWindows As SHDocVw.ShellWindows  
            Dim hwnd As IntPtr  
            Dim ieDesktop As SHDocVw.InternetExplorer  
            oShell = New Shell32.Shell  
            oWindows = oShell.Windows()  
            ieDesktop = oWindows.FindWindowSW(0, vbNull, 8, hwnd, 1)  
            DesktopFolderView = DirectCast(ieDesktop.Document, Shell32.ShellFolderView)  
        End Sub  
    End Class  
      
    
    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2020-12-01T18:19:22.623+00:00

    Hi
    Try this:

    ListBox1.Items.AddRange(IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)))
    

  2. Karen Payne MVP 35,026 Reputation points
    2020-12-01T20:44:23.18+00:00

    Hello @jenCarlos ,

    I would think a ListView would be a better option as shown here which even has cancellation option. To try this out see OtherExamplesForm.vb in my GitHub repository.

    Note the first column is wide because on my computer there are many folders that are deeply nested. Also if needed will not crash if an access denied is thrown.

    44221-traverse1.png

    Full source

    Imports System.IO  
    Imports System.Threading  
    Imports RecurseFolders.Extensions  
      
    Public Class OtherExamplesForm  
        ''' <summary>  
        ''' Provides an opportunity to cancel traversal of folders  
        ''' </summary>  
        Private _cts As New CancellationTokenSource()  
        Private _exceptionList As List(Of String)  
      
        ''' <summary>  
        ''' Traverse folders searching for any .dll files  
        ''' </summary>  
        ''' <param name="sender"></param>  
        ''' <param name="e"></param>  
        Private Async Sub DeniedAccessCoveredButton_Click(sender As Object, e As EventArgs) Handles DeniedAccessCoveredButton.Click  
      
            If _cts.IsCancellationRequested = True Then  
                _cts.Dispose()  
                _cts = New CancellationTokenSource()  
            End If  
      
            _exceptionList = New List(Of String)  
            ExceptionsListBox.DataSource = Nothing  
      
            Dim stack As New Stack(Of String)  
      
            stack.Push(Environment.GetFolderPath(Environment.SpecialFolder.Desktop))  
      
            Try  
      
                Await Task.Run(Async Function()  
      
                                   Do While (stack.Count > 0)  
                                       Dim directory As String = stack.Pop  
                                       Dim lvItem As New ListViewItem(directory)  
                                       ListView1.InvokeIfRequired(Sub(lv) lv.Items.Add(lvItem))  
      
                                       Await Task.Delay(1)  
      
                                       If _cts.IsCancellationRequested Then  
                                           _cts.Token.ThrowIfCancellationRequested()  
                                       End If  
      
                                       Try  
      
                                           Dim files = IO.Directory.GetFiles(directory, "*.*")  
      
                                           If files.Length > 0 Then  
      
                                               For Each file As String In files  
                                                   Dim item = New ListViewItem(New String() {"", Path.GetFileName(file)})  
                                                   ListView1.InvokeIfRequired(Sub(lv) lv.Items.Add(item))  
                                               Next  
      
                                           End If  
      
                                           Dim strDirectoryName As String  
                                           For Each strDirectoryName In IO.Directory.GetDirectories(directory)  
                                               stack.Push(strDirectoryName)  
      
                                           Next  
      
                                       Catch ex As UnauthorizedAccessException  
                                           _exceptionList.Add(ex.Message)  
                                       End Try  
                                   Loop  
      
                               End Function)  
      
                ExceptionsListBox.DataSource = _exceptionList  
      
                ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)  
                ListView1.FocusedItem = ListView1.Items(0)  
                ListView1.Items(0).Selected = True  
                ActiveControl = ListView1  
      
            Catch ex As OperationCanceledException  
                MessageBox.Show($"Cancelled")  
            End Try  
      
        End Sub  
      
        Private Sub CancelButton_Click(sender As Object, e As EventArgs) Handles CancelButton.Click  
            _cts.Cancel()  
        End Sub  
    End Class  
    

  3. Anonymous
    2020-12-01T23:36:52.73+00:00

    Hi
    Here is an alternative. Use a OpenFileDialog and let User select files then add those to the ListBox.
    Not quite what you want, but maybe worth a try.

            Using ofd As New OpenFileDialog
                With ofd
                    .Multiselect = True
                    .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
                    If .ShowDialog = DialogResult.OK Then
                        ListBox1.Items.AddRange(.FileNames)
                    End If
                End With
            End Using