1、属性设置
AllowDrop 属性设为True 允许拖拽
2 加入事件 对话框添加DragDrop和DragEnter属性
private void Form1_DragEnter(object sender, DragEventArgs e) //获得“信息”
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.All; //重要代码:表明是所有类型的数据,比如文件路径
else
e.Effect = DragDropEffects.None;
}
private void Form1_DragDrop(object sender, DragEventArgs e) //解析信息 单个文件解析方式
{
string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString(); //获得路径
textBox1.Text = path; //由一个textBox显示路径
}
多个文件解析模式
string[] aa = ((string[])e.Data.GetData(DataFormats.FileDrop));
foreach (string one in aa)
{
ListViewItem items = new ListViewItem();
items.Text = one;
this.listView1.Items.Add(items);
}