❝
The initial public account is Dotnet9
Author: The Wolf at the end of the desert
Date: 202-11-27
First, before the beginning of this article
When uploading a file, an upload button is generally provided. Click upload, and the dialog box for selecting a file (or directory) pops up. After selecting a file (or directory), obtain the file path from the dialog box and upload the file.
Dialog box to select a file
The selection dialog box code is as follows:
OpenFileDialog openFileDialog = new OpenFileDialog(); Openfiledialog. Title = "select Exe file "; OpenFileDialog. Filter = "exe file | *. 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;Copy the code
But in general, the best user experience is to directly mouse drag and drop files:
Baidu network disk drag upload files
The following is a brief description of the implementation of file drag and drop in WPF.
How to drag and drop files in WPF?
It’s as simple as dragging and dropping the receiving control (or container) to register two events: DragEnter and Drop.
Let’s look at my implementation:
Drag and drop files into QuickApp
Register events in Xaml
Registration event:
<Grid MouseMove="Grid_MouseMove" AllowDrop="True" Drop="Grid_Drop" DragEnter="Grid_DragEnter">
Copy the code
Event handling methods:
-
Grid_DragEnter 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: Handles drag-and-drop file operations
- Grid_Drop method
This is the way to handle actual drag and drop, get the drag and drop file path (if it is the operating system file shortcut (extension called LNK), you need to use com components (not the paper explain the key, specific see this open source project) to obtain the actual file path), can handle the subsequent operations (such as 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 }; 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); }}Copy the code
This passage is Over
Function is very simple, not profound, can use on the line.
❝
Time is like water, can only flow to not flow back.
The initial public account is Dotnet9
Author: The Wolf at the end of the desert
Date: 202-11-27