I have a Windows Forms TextBox in which I want to allow the user to drag and drop a file from Windows Explorer.
I would like to allow only for dropping an .xml file (path) on the TextBox.
The way of testing the file format to be dropped, on the DragEnter event, is:
private void DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Move;
else
e.Effect = DragDropEffects.None;
}
The DataFormats above doesn’t contain Xml. If I use DataFormats.FileDrops, I allow any types of files to be dropped, as far as I understood.
Any ideas?
Thanks in advance!
you should check the
DataFormats.FileDropthen get the file name(s) and verify the file extension, you can then set theDragDropEffectsas you wish depending on how many files are dropped and their extension (in your case only 1 and xml file extension).check this answer with a working example: https://stackoverflow.com/a/736883/559144