1. Before Starting This Article
When uploading files, the typical approach is to provide an upload button, click to upload, pop up a file (or folder selection) dialog, select the file (or folder), obtain the file path from the dialog object, and then proceed with the upload operation.
Select file via dialog

The dialog selection code is as follows:
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "Select Exe File";
openFileDialog.Filter = "Executable files|*.exe";
openFileDialog.FileName = string.Empty;
openFileDialog.FilterIndex = 1;
openFileDialog.Multiselect = false;
openFileDialog.RestoreDirectory = true;
openFileDialog.DefaultExt = "exe";
if (openFileDialog.ShowDialog() == false)
{
return;
}
string txtFile = openFileDialog.FileName;
However, in general, the best user experience is to directly drag and drop files:
Baidu Netdisk drag-and-drop upload

Below is a brief explanation of how to implement file drag-and-drop in WPF.
2. How to Drag and Drop Files in WPF?
It's actually very simple: just register two events on the drag-and-drop target control (or container): DragEnter and Drop.
Take a look at my implementation effect:
Drag file into QuickApp

Register Events in XAML
Register events:
<Grid
MouseMove="Grid_MouseMove"
AllowDrop="True"
Drop="Grid_Drop"
DragEnter="Grid_DragEnter"
></Grid>
Event Handling Methods:
- Grid_DragEnter handling method
private void Grid_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effects = DragDropEffects.Link;
}
else
{
e.Effects = DragDropEffects.None;
}
}
DragDropEffects.Link: Handle drag-and-drop file operation
- Grid_Drop handling method
This is the method that processes the actual drag-and-drop operation. After obtaining the dragged file path (if it's a shortcut file (with .lnk extension), you need to use a COM component (this is not the focus of this article; see the open-source project for details) to get the actual file path), you can then proceed with subsequent operations (e.g., file upload).
private void Grid_Drop(object sender, DragEventArgs e)
{
try
{
var fileName = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
MenuItemInfo menuItem = new MenuItemInfo() { FilePath = fileName };
// For shortcuts, we need to retrieve the target file path
if (fileName.ToLower().EndsWith("lnk"))
{
WshShell shell = new WshShell();
IWshShortcut wshShortcut = (IWshShortcut)shell.CreateShortcut(fileName);
menuItem.FilePath = wshShortcut.TargetPath;
}
ImageSource imageSource = SystemIcon.GetImageSource(true, menuItem.FilePath);
System.IO.FileInfo file = new System.IO.FileInfo(fileName);
if (string.IsNullOrWhiteSpace(file.Extension))
{
menuItem.Name = file.Name;
}
else
{
menuItem.Name = file.Name.Substring(0, file.Name.Length - file.Extension.Length);
}
menuItem.Type = MenuItemType.Exe;
if (ConfigHelper.AddNewMenuItem(menuItem))
{
AddNewMenuItem(menuItem);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
3. End of This Article
The functionality is very simple; no need to delve too deep, just knowing how to use it is enough.