我认为我准确地设置了这个设置,但是当我尝试将文件拖到我的表单上时,它只显示一个带有一条直线的圆圈。哪件作品没有正确设置?
private void Form1(object sender, EventArgs e)
{
this.AllowDrop = true;
this.DragDrop += new DragEventHandler(Form1_DragDrop);
this.DragEnter += new DragEventHandler(Form1_DragEnter);
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent(DataFormats.FileDrop)) return;
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (var file in files)
{
var ext = Path.GetExtension(file);
if (ext.Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase))
{
e.Effect = DragDropEffects.Copy;
MessageBox.Show(sender.ToString());
return;
}
else { MessageBox.Show("This filetype is not allowed");
}
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent(DataFormats.FileDrop)) return;
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (var file in files)
{
var ext = Path.GetExtension(file);
if (ext.Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase))
{
e.Effect = DragDropEffects.Copy;
MessageBox.Show(sender.ToString());
return;
}
else { MessageBox.Show("This filetype is not allowed");
}
}
编辑代码
答案 0 :(得分:1)
这对我有用......
public partial class Form1 : Form
{
public Form1()
{
this.InitializeComponent();
this.AllowDrop = true;
this.DragDrop += new DragEventHandler(Form1_DragDrop);
this.DragEnter += new DragEventHandler(Form1_DragEnter);
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (var file in files)
{
MessageBox.Show(file);
}
}
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
}
}
从Windows资源管理器/我的桌面等删除任何文件会显示一个带有路径和文件名的消息框