1
votes

I am trying to drag a text file into my C# WPF application to capture the file location. The things I have tried so far are as follows:

  1. Set the AllowDrop property to true
  2. Add the DragEnter, DragOver and Drop events - none of them fire - all i get is a black circle with a line through it which, i think means, unavailable.
  3. Added the following to the app manifest level="requireAdministrator" uiAccess="false" />

I have utilised the OpenFileDialog method as an alternative but would be nice to have both options.

1
could you show some code please? and you want just drop the name of file but drop in what?? - Frenchy

1 Answers

3
votes

You could handle the PreviewDragOver and Drop events. Below is an example of a WPF TextBox on which you can drop a file from the file explorer. The path of the first dropped file will appear in the TextBox.

private void TextBox_Drop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        string[] files = e.Data.GetData(DataFormats.FileDrop) as string[];
        if (files != null && files.Length > 0)
        {
            ((TextBox)sender).Text = files[0];
        }
    }
}

private void TextBox_PreviewDragOver(object sender, DragEventArgs e)
{
    e.Handled = true;
}

XAML:

<TextBox AllowDrop="True" PreviewDragOver="TextBox_PreviewDragOver" Drop="TextBox_Drop" />