C# drag and drop files to form
c#, drag-and-drop, winforms
Solution
This code will loop through and print the full names (including extensions) of all the files dragged into your window:
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string filePath in files)
{
Console.WriteLine(filePath);
}
}
Problem
How could I load files to a form by drag-and-drop? Which event will appear? Which component should I use? And how to determine name of file and others properties after drag-and-dropping it to a form? Thank you! Code ``` private void panel1_DragEnter(object sender, DragEventsArgs e){ if (e.Data.GetDataPresent(DataFormats.Text)){ e.Effect = DragDropEffects.Move; MessageBox.Show(e.Data.GetData(DataFormats.Text).toString()); } if (e.Data.GetDataPresent(DataFormats.FileDrop)){ } } ``` ok, this works. How about files? How can I get filename and extension? and this is only a `dragEnter` action.