简体   繁体   中英

How to get the file name when dragging a file(before dropping the file) in windows?

I'm developing a WPF application. I will do a drag and drop in this app but I need to get the file's name(or path) when dragging the file(without dropping). I think this may related to some shell programming. Does anyone know how to realize it?

What do you mean by 'get the file name when dragging a file'? Are you asking how to automatically copy the name of a file when you drag it into your WPF application?

  • To get the file name: Try clicking once on the file icon, then click again on the file's name about 2-3 sec later. The name will become an editable text field and you can Copy the name before dragging, to be pasted into the WPF application.

  • To get the file path: In Windows Explorer (file browser), browse to the file you want to drag. In the bar at the top of the window where it shows the folders you've gone through, right-click and select "Copy address as text". You can then Ctrl+V the filepath into any text field.

It's kind of hard to understand what you're asking (kind of tired here), sorry if my answer didn't help in the slightest haha.

   private void MyControl_DragOver(object sender, DragEventArgs e) {
        e.Effects = DragDropEffects.None;
        if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
            string[] paths = e.Data.GetData(DataFormats.FileDrop) as string[];
            if(paths.Lenght > 0 && File.Exists(paths[0])) { // <- example, you must handle it in your way
                e.Effects = DragDropEffects.Copy | DragDropEffects.Move;
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM