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 -
Add references to the project as shown here -
In solution explorer the highlighted references should be present -
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.