How can i get desktop selected folder path or name

Alparslan Avlamaz 1 Reputation point
2022-09-02T21:26:50.06+00:00

Hello

How can I get the path or name of a folder I selected on the desktop with the mouse?

With C#

Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RLWA32 49,541 Reputation points
    2022-09-03T10:05:40.137+00:00

    A simple example using a .Net Framework 4.8 winform project created with VS2022 --

    Add a listbox, a checkbox and a button to a form. For example -

    237502-form.png

    Add references to the project as shown here -

    237466-references.png

    In solution explorer the highlighted references should be present -

    237419-solution.png

    Edit the code for the form as shown here (make sure to add the handler for the Form's Load event) -

    using System;  
    using System.Collections.Generic;  
    using System.ComponentModel;  
    using System.Data;  
    using System.Drawing;  
    using System.Linq;  
    using System.Text;  
    using System.Threading.Tasks;  
    using System.Windows.Forms;  
    using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;  
      
    namespace DeskSelected  
    {  
        public partial class Form1 : Form  
        {  
            Shell32.ShellFolderView desktopFolderView;  
            public Form1()  
            {  
                InitializeComponent();  
            }  
      
            private void button1_Click(object sender, EventArgs e)  
            {  
                listBox1.Items.Clear();  
                Shell32.FolderItems selItems = desktopFolderView.SelectedItems();  
                foreach (Shell32.FolderItem fitem in selItems)  
                {  
                    if (checkBox1.Checked)  
                    {  
                        if (fitem.IsFileSystem)  
                            listBox1.Items.Add(fitem.Path);  
                    }  
                    else  
                        listBox1.Items.Add(fitem.Name);  
                }  
            }  
      
            private void Form1_Load(object sender, EventArgs e)  
            {  
                int hwnd;  
                Shell32.Shell iShell = new Shell32.Shell();  
                SHDocVw.ShellWindows iWindows = iShell.Windows();  
                SHDocVw.InternetExplorer iDesktop = iWindows.FindWindowSW(0, null, 8, out hwnd, 1);  
                desktopFolderView = (Shell32.ShellFolderView)iDesktop.Document;  
            }  
        }  
    }  
      
    

    Build the project and run the windows form application.

    After selecting items on the desktop click the "Get Selected" button on the form. The selected items should appear in the listbox. If the "File System Paths" checkbox is checked the file system paths for selected items will be displayed. Not everything on the desktop is a file system object so items like the "Recycle Bin" will not appear when displaying file system paths.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.